码迷,mamicode.com
首页 > 其他好文 > 详细

利用expect来实现非交互式备份现网网络设备配置

时间:2020-04-27 15:10:06      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:system   备份   roo   unp   步骤   update   cisco   body   网络技术   

环境:
网络设备(客户端):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

利用expect来实现非交互式备份现网网络设备配置

标签:system   备份   roo   unp   步骤   update   cisco   body   网络技术   

原文地址:https://blog.51cto.com/14648383/2490649

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!