简单操作如下
expect echohostname.sh
cat echohostname.sh
#########################################################################
# File Name: echohostname.sh
# Author: dty
# mail: 576786031@qq.com
# Created Time: Mon 27 Jul 2015 12:50:18 PM CST
#########################################################################
#!/usr/bin/expect
foreach ip { //括号前后注意空格!!!!!!!!!!
172.25.254.4
172.25.254.5
172.25.254.6
} {
spawn ssh root@$ip
expect "*password"
sleep 2 //没这延迟就犯2!!!!!
send "redhat\r"
expect "root"
send "echo `hostname` > /var/www/html/index.html\r"
expect eof
}
如果你之前没有连过ssh那么还会出现yes/no
这个时候采用下面这个脚本即可
7 #!/usr/bin/expect
8 foreach ip {
9 172.25.254.4
10 172.25.254.5
11 172.25.254.6
12 } {
13 spawn ssh root@$ip
14 expect "(yes/no)" {
15 send "yes\r"
16 expect "password"
17 sleep 2
18 send "redhat\r"
19 } "*password"
20 sleep 2
21 send "redhat\r"
22 expect "root"
23 send "echo `hostname` > /var/www/html/index.html\r"
24 expect "#"
25 send "logout\r"
26 expect eof
27 }
下面这个测试比较稳定相信还可以做优化,初学