linux下交互式登录工具:expect
需要自己安装
yum install expect
安装完成即可,expect功能非常强大……
脚本代码如下
iplist.dat为设备IP文件
serverlist.dat为tftp备份服务器IP文件
这样添加设备或者更改tftp服务器IP的话只需要修改这2个文件即可,不需要修改脚本。
将该脚本放到计划任务中执行,将输出结果重定向到/dev/null。
#!/usr/bin/expect -f
set IPlist [exec cat /tmp/iplist.dat]
set Serverlist [exec cat /tmp/serverlist.dat]
foreach Ser $Serverlist {
foreach IP $IPlist {
spawn telnet $IP
expect "Username:"
send "username\n"
expect "Password:"
send "******\n"
expect "#"
send "copy running-config tftp:\n"
expect "Address or name of remote host"
send "$Ser\n"
expect "Destination filename"
send "\n"
expect "#"
send "exit\n"
expect eof
}
}