转自
http://segmentfault.com/a/1190000000460399

公司的网络设备越来越多,得写个批量备份脚本了,要不备份配置文件累死了.本来想用Ruby的Net::Telnet,后来还是简单点用expect实现了.

#!/usr/bin/expect

# h3c config backup
# venmos
# venmos.com
# me[at]venmos.com

set device1 "192.168.1.1"
set device2 "192.168.2.1"
set user user  
set pass password
set timeout 60

spawn telnet $device1
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"

expect ">"
send "tftp (tftp server ip) put startup.cfg device1.cfg \n"
expect ">"
send "\n"
send "exit\n"

spawn telnet $device2
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"

expect ">"
send "tftp (tftp server ip) put startup.cfg device2.cfg \n"
expect ">"
send "\n"
send "exit\n"

把脚本放到一台服务器上定时执行,在配合Git就可以增量备份了,以后网络设备不小心配置错误也不用担心了,可以随时用Git回滚恢复.