except代码如下:
=== except.code===
#!/usr/bin/expect -f
# Set variables
set hostname [lindex $argv 0]
set username "user"
set passwd "password1!"
# Log results
log_file -a ~/results.log
# Announce which device we are working on and at what time
send_user "\n"
# Don‘t check keys
spawn ssh -c 3des -x -o StrictHostKeyChecking=no -l jiaqiang_tian $hostname
# Allow this script to handle ssh connection issues
expect {
timeout { send_user "\nTimeout Exceeded - Check Host\n"; exit 1 }
eof { send_user "\nSSH Connection To $hostname Failed\n"; exit 1 }
"*#" {}
"*assword:" {
send "$passwd\r"
}
}
expect "#"
send "show vstack config\r"
expect "#"
send "conf ter\r"
expect "#"
send "no vstack\r"
expect "#"
send "end\r"
expect "#"
send "copy running-config startup-config\r"
expect "*onfig]? "
send "\r"
expect "#"
send "exit\n"
使用方法:
./except.coode hostname/IP
关于Password中特殊字符的说明:
如果密码中有&,#,$的, 处理的方式就是使用转移符\,比如说&-> \&
其他的符号应该不用转义,比如说:!@%^*
原文:http://blog.51cto.com/scantydd/2105603