Linux定時備份交換機配置檔案
阿新 • • 發佈:2018-11-11
場景:交換機數量幾百臺,需要定時備份配置檔案以防丟失。
最初方案:從Linux上寫一個expect指令碼,定時登入到交換機,手動執行檢視配置檔案(show run),並記錄日誌。
最開始寫了一個expect指令碼,內容如下:
#!/usr/bin/expect set date [exec date "+%Y%m%d%H%M%S"] set timeout 10 spawn telnet 192.168.1.1 expect "Username:" send "Telnet_username\r" expect "Password:" send "Telnet_Password\r" send "enable\r" expect "Password:" send "enable_password\r" send "show run\r" send " \r" send "exit\r" interact
執行此指令碼,重定向輸出內容到檔案
./192.168.1.1.exp > test.txt
使用cat檢視,沒有問題 ,但是使用vim編輯,或者轉移到Windows檢視該檔案,會有很多^M;執行file test.txt檢視該檔案型別,
! ! ! aaa new-model ! ! ! aaa accounting update periodic 15 aaa accounting network default start-stop group compatible aaa authentication dot1x default group radius --More-- ^H^H^H^H^H^H^H^H^H^H ^H^H^H^H^H^H^H^H^H^H! ! nfpp ! !
[[email protected] expect]# file test.txt
test.txt: ASCII text, with CRLF, CR line terminators, with overstriking
檔案型別為二進位制檔案,想了很多辦法,嘗試使用dos2unix,無果。
[[email protected] expect]# dos2unix test.txt
dos2unix: Binary symbol found at line 43
dos2unix: Skipping binary file test.txt
不考慮,直接放棄該方法,轉而使用另一種方法:通過tftp上傳到Linux的tftp伺服器上面。
1、安裝tftp相關軟體包
[[email protected] ~]# yum install tftp-server xinetd -y
2、修改/etc/xinetd.d/tftp檔案
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /tftp 注:修改此處,-s指定目錄,/tftp為你需要的tftp共享目錄,-c允許上傳
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
3、重啟tftp服務
由於tftp使用udp 69埠,所以需要在防火牆開放此埠。
service xinetd restart
4、建立/tftpboot,並賦予讀寫許可權。
[[email protected] /]# chmod 705 tftp/
[[email protected] /]# telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
User Access Verification
Username:user
Password:
Ruijie_Switch>enable
Password:
Ruijie_Switch#copy startup-config tftp:
Address of remote host []?192.168.1.2
Destination filename [/config.text]?
Accessing startup-config...
Transmission finished, file length 12599 bytes.
Ruijie_Switch#exit
[[email protected] /]# cd /tftp/
[[email protected] tftp]# ls
config.text
至此成功備份交換機配置檔案到伺服器上,只要寫一個crontab定時執行就可以定時備份。