用expect写的一个脚本
时间:2014-06-26 00:57 来源:linux.it.net.cn 作者:IT网
用expect写的一个脚本,配合for循环执行,因为怕与expect符号冲突,所以在调用awk的时候没写太复杂的脚本,像替换我都用管道给sed来完成。
供大家参考学习。
复制代码代码如下:
#!/usr/bin/expect -f
set ip [lindex $argv 0]
set timeout 5
spawn ssh root@$ip
expect {
"(yes/no)?" {send "yes\r"}
"*password:" {send "123456@\r"}
}
expect "password:"
send "123456@\r"
#interact #留在子进程
expect "#"
send "ifconfig eth0 |awk '\$1 ~ /inet/ { print \$2}' > /tmp/$ip.txt\r"
expect "#"
send "exec uptime|awk '{print \$1,\$10}'| sed 's!,!!g' >> /tmp/$ip.txt\r"
spawn /usr/bin/scp root@$ip:/tmp/$ip.txt /home
expect "password:"
send "123456@\r"
expect "~]# "
#send "exit\r"
(责任编辑:IT)
用expect写的一个脚本,配合for循环执行,因为怕与expect符号冲突,所以在调用awk的时候没写太复杂的脚本,像替换我都用管道给sed来完成。
复制代码代码如下:
#!/usr/bin/expect -f
(责任编辑:IT)set ip [lindex $argv 0] set timeout 5 spawn ssh root@$ip expect { "(yes/no)?" {send "yes\r"} "*password:" {send "123456@\r"} } expect "password:" send "123456@\r" #interact #留在子进程 expect "#" send "ifconfig eth0 |awk '\$1 ~ /inet/ { print \$2}' > /tmp/$ip.txt\r" expect "#" send "exec uptime|awk '{print \$1,\$10}'| sed 's!,!!g' >> /tmp/$ip.txt\r" spawn /usr/bin/scp root@$ip:/tmp/$ip.txt /home expect "password:" send "123456@\r" expect "~]# " #send "exit\r" |