标签:expect
#!/usr/bin/expect
set host 192.168.1.124
set user root
set password "root"
spawn ssh $user@$host
expect "yes/no" { send "yes\r" }
expect "password:" { send "$password\r" }
expect "\[$user\@" { send "pwd > /tmp/$user.txt ; exit\r" }
interact
------------------------------------------------------------->脚本传参$1 = set host [lindex $argv 0]
#!/usr/bin/expect
set host [lindex $argv 0]
set user root
set password "root"
spawn ssh $user@$host
expect "yes/no" { send "yes\r" }
expect "password:" { send "$password\r" }
expect "\[$user\@" { send "echo ‘my name is you!‘ > /tmp/$user.txt ; exit\r" }
interact
------------------------------------------------------------------>
#!/bin/bash
known_file=/root/.ssh/known_hosts
rm -rf ${known_file:-/tmp/*}
expect <<EOF
set timeout 5
spawn ssh root@192.168.4.8
expect "password" {send "123456\r"}
expect "#" {send "echo ‘my name is you‘ >/tmp/root7.txt\r"}
expect "#" {send "exit\r"}
EOF
------------------------------------------------------->给254台安装httpd服务
#!/bin/bash
known_file=/root/.ssh/known_hosts
rm -rf ${known_file:-/tmp/*}
for i in $(seq 254)
do
expect <<EOF
spawn ssh root@192.168.4.$i
expect "yes/no" {send "yes\r"}
expect "password" {send "123456\r"}
expect "#" {send "echo ‘yum -y install httpd\r"}
expect "#" {send "exit\r"}
EOF
done
------------------------------------------------------>给254发密钥 ssh-copy-id
#!/bin/bash
for i in $(seq 254)
do
expect <<EOF
spawn ssh-copy-id root@192.168.4.$i
expect "yes/no" {send "yes\r"}
expect "password" {send "123456\r"}
expect "#" {send "exit\r"}
EOF
done
本文出自 “12177655” 博客,谢绝转载!
标签:expect
原文地址:http://12187655.blog.51cto.com/12177655/1974022