使用expect脚本实现网络设备配置的自动备份与存档
时间:2009-04-03 来源:liyf0371
代码
1. #!/usr/bin/expect -f
2.
3. if { $argc != 6 } {
4. send_user "usage: get_hwsw6503_conf.exp ip user password swip swuser swpassword\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. set SWSERVER [lindex $argv 3]
16. set SWUSERNAME [lindex $argv 4]
17. set SWPASSWD [lindex $argv 5]
18.
19. spawn telnet
20. expect "telnet> "
21. send "open $SERVER\r"
22. sleep 1
23. expect "Username:"
24. send "$USERNAME\r"
25. sleep 1
26. expect "Password:"
27. send "$PASSWD\r"
28. sleep 1
29. expect "*>"
30.
31. send "telnet $SWSERVER\r"
32. sleep 1
33. expect "Username:"
34. send "$SWUSERNAME\r"
35. sleep 1
36. expect "Password:"
37. send "$SWPASSWD\r"
38. sleep 1
39. expect "*>"
40.
41. send "disp cur\r"
42.
43. while (1) {
44. sleep 1
45. expect {
46. " ---- More ----" { send " " }
47. "<S6*>" { break }
48. }
49. }
50. send "quit\r"
51. sleep 1
52. expect "<NE*>"
53. send "quit\r"
54.
55. interact
4、抓取并ftp到指定服务器的shell脚本:go.sh ,和上述脚本存放在同一目录下
代码
1. #!/bin/sh
2.
3. PATH="/sbin:/usr/sbin:/bin:/usr/bin"
4. export PATH
5.
6. #取系统当前时间,用来和设备IP组合起来做存档的文件名,格式:IP_日期时间.txt
7. T=`date +%Y%m%d%H%M`
8.
9. #cisco路由器的地址和密码
10. cisco_rt="1.1.1.1"
11. cisco_rt_para="password enpass"
12. #抓取配置并进行适当的处理滤掉各种控制字符
13. ./get_cisco_conf.exp $cisco_rt $cisco_rt_para | sed 's/ --More-- //g'| sed 's/\cM//g' | sed 's/\cH//g' | sed 's/ //g' > $cisco_rt"_"$T.txt
14.
15. #Huawei路由器的地址和用户名、密码
16. huawei_rt="2.2.2.2"
17. huawei_rt_para="user pass"
18. #抓取配置并进行适当的处理滤掉各种控制字符
19. ./get_hwrtne_conf.exp $huawei_rt $huawei_rt_para | sed 's/\cM//g' | sed 's/ //g' | sed 's/---- More ----//g' | sed 's/\cG//g' | sed 's/[42D//g' > $huawei_rt"_"$T.txt
20.
21. #Huawei交换机的地址和用户名、密码
22. huawei_sw="3.3.3.3"
23. huawei_sw_para="user pass"
24. #抓取配置并进行适当的处理滤掉各种控制字符
25. ./get_hwsw6503_conf.exp $huawei_rt $huawei_rt_para $huawei_sw $huawei_sw_para | sed 's/\cM//g' | sed 's/ //g' | sed 's/---- More ----//g' | sed 's/\cG//g' | sed 's/[42D//g' > $huawei_sw"_"$T.txt
26.
27. #自动登录ftp服务器上传文件
28. ftp -nui<<EOF
29. open ftp.server.ip
30. user sysbackup password
31. asc
32. mput *_$T.txt
33. close
34. by
35. EOF
36.
37. #删除临时文件
38. rm -f *_$T.txt