利用expect來實現非交互式備份現網網絡設備配置

環境:
網絡設備(客戶端):192.168.1.11;192.168.1.12
tftp sever:192.168.1.50
Linux 備份服務器:192.168.1.50

操作前:
1)、關閉防火牆及Selinux
2)、提前配置好網絡設備telnet功能,同時確保該賬戶有tftp 備份設備配置文件的權限。如下案例中網絡設備作爲客戶端的telnet賬戶及密碼均爲admin

操作步驟:
1、安裝tftp server、 xinetd 、expect
yum install -y xinetd tftp-server
2、編輯xinetd下的tftp-server
配置tftp server
vim /etc/xinetd.d/tftp
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot -c
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}

重啓xinet服務並確認tftp server是否啓動
service xinetd restart
netstat -tlunp | grep 69
udp 0 0 0.0.0.0:69 0.0.0.0:* 1499/xinetd

3、編輯expect腳本
vim back_expect.exp
set timeout 60
set ip [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
spawn telnet $ip
expect -re "login:|Username:"
send "$username\r"
expect "Password:"
send "$password\r"
expect ">"
send "tftp 192.168.1.50 put startup.cfg\r"
expect "
>"
send "quit\r"
expect eof

編輯備份H3C設備配置的腳本
vim h3c_back.sh
#!/bin/bash
while read ip username password
do
/usr/bin/expect /script/data/backexpect.exp $ip $username $password > /dev/null
if [ -e /var/lib/tftpboot/startup.cfg ];then
mv /var/lib/tftpboot/startup.cfg /tmp/$(date +%F)
${ip}.cfg
echo "$ip put sucess"
else
echo "$ip backup failed"
fi
done </tmp/1.txt

網絡設備的地址、telnet賬戶、telnet密碼
/tmp/1.txt
192.168.1.11 admin admin
192.168.1.12 admin admin

執行結果
sh h3c_back.sh

ll /tmp/
總用量 72
-rw-rw-rw- 1 nobody nobody 6623 12月 17 00:17 2019-12-17_192.168.1.11.cfg
-rw-rw-rw- 1 nobody nobody 6617 12月 17 00:17 2019-12-17_192.168.1.12.cfg

確認備份目錄下的網絡設備配置文件是否正確
less /tmp/2019-12-17_192.168.1.11.cfg

version 7.1.075, H3C

sysname R1

telnet server enable

irf mac-address persistent timer
irf auto-update enable
undo irf link-delay
irf member 1 priority 1

lldp global enable

system-working-mode standard
xbar load-single
password-recovery enable
lpu-type f-series

vlan 1

interface NULL0

interface LoopBack0
ip address 1.1.1.1 255.255.255.255

interface Vlan-interface1
ip address 192.168.1.11 255.255.255.0


同理,備份Cisco、HUAWEI設備方法類似,這裏不再一 一舉例,具體腳本可參考如下:

HUAWEI:

vim hw_back_expect.exp
#!/usr/bin/expect
set timeout 60
set ip [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
spawn telnet $ip
expect -re "login:|Username:"
send "$username\r"
expect "Password:"
send "$password\r"
expect ">"
send "tftp 192.168.1.50 put vrpcfg.zip\r"
expect "
>"
send "quit\r"
expect eof

vim hw_back.sh
#!/bin/bash
while read ip username password
do
/usr/bin/expect /script/data/hw_backexpect.exp $ip $username $password > /dev/null
if [ -e /var/lib/tftpboot/vrpcfg.zip ];then
mv /var/lib/tftpboot/vrpcfg.zip /tmp/$(date +%F)
${ip}.cfg
echo "$ip put sucess"
else
echo "$ip backup failed"
fi
done </tmp/2.txt


Cisco:

cisco_expect.exp
#!/usr/bin/expect
set timeout 60
set ip [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
spawn telnet $ip
expect -re "login:|Username:"
send "$username\r"
expect "Password:"
send "$password\r"
expect "#"
send "copy startup-config tftp://192.168.1.50/$ip.cfg\r"
send "\r"
send "\r"
expect "
end"
send "exit\r"
expect eof

vim cisco_back.sh
#!/bin/bash
while read ip username password
do
/usr/bin/expect /script/data/ciscoexpect.exp $ip $username $password > /dev/null
if [ -e /var/lib/tftpboot/${ip}.cfg ];then
mkdir -p /tmp/$(date +%F)
mv /var/lib/tftpboot/${ip}.cfg /tmp/$(date +%F)/$(date +%F)
${ip}.cfg
echo "$ip put sucess"
else
echo "$ip backup failed"
fi
done </tmp/3.txt


初學shell,腳本難免有錯誤之處,歡迎大家指正! -------一隻初學Linux的網絡運維工程師,網絡技術問題可聯繫QQ:1656209309

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章