使用expect脚本实现网络设备配置的自动备份与存档(1)
时间:2009-04-03 来源:liyf0371
目标:
利用expect脚本自动抓取网络设备的配置数据,并保存到指定的ftp服务器上
目标:
利用expect脚本自动抓取网络设备的配置数据,并保存到指定的ftp服务器上
准备工作:
1、一台Linux主机,确认已经安装了expect软件包,expect的文档到google上取查
2、一个已搭建好的ftp服务器
实例:
1台Cisco路由器,1台Huawei NE系列路由器和1台Huawei 6503交换机,并且6503不能直接telnet,需要通过NE路由器做跳板登录
脚本:
1、抓取Cisco路由器配置的脚本:get_cisco_conf.exp
代码
1. #!/usr/bin/expect -f
2.
3. if { $argc != 3 } {
4. send_user "usage: get_cisco_conf.exp ip password enablepassword\n"
5. exit
6. }
7.
8. set timeout 1
9. set TERM ANSI
10.
11. set SERVER [lindex $argv 0]
12. set PASSWD [lindex $argv 1]
13. set ENPASSWD [lindex $argv 2]
14.
15. spawn telnet
16. expect "telnet> "
17. send "open $SERVER\r"
18. sleep 1
19. expect "Password:"
20. send "$PASSWD\r"
21. sleep 1
22. expect "*>"
23. send "en\r"
24. sleep 1
25. expect "Password:"
26. send "$ENPASSWD\r"
27. sleep 1
28. expect "*#"
29.
30. send "show run\r"
31. while (1) {
32. sleep 1
33. expect {
34. " --More-- " { send " " }
35. "*#" { break }
36. }
37. }
38. send "exit\r"
39.
40. interact
2、抓取Huawei NE路由器配置的脚本:get_hwrtne_conf.exp
代码
1. #!/usr/bin/expect -f
2.
3. if { $argc != 3 } {
4. send_user "usage: get_hwrtne_conf.exp ip user password\n"
5. exit
6. }
7.
8. set timeout 1
9. set TERM ANSI
10.
11. set SERVER [lindex $argv 0]
12. set USERNAME [lindex $argv 1]
13. set PASSWD [lindex $argv 2]
14.
15. spawn telnet
16. expect "telnet> "
17. send "open $SERVER\r"
18. sleep 1
19. expect "Username:"
20. send "$USERNAME\r"
21. sleep 1
22. expect "Password:"
23. send "$PASSWD\r"
24. sleep 1
25. expect "*>"
26.
27. send "disp cur\r"
28. while (1) {
29. sleep 1
30. expect {
31. " ---- More ----" { send " " }
32. "<NE*>" { break }
33. }
34. }
35. send "quit\r"
36.
37. interact