Linux定时备份交换机配置文件

版权声明:欢迎转载,转载请附上链接。 https://blog.csdn.net/qq_41571056/article/details/83059373

场景:交换机数量几百台,需要定时备份配置文件以防丢失。

最初方案:从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
!
!
[root@Zabbix93 expect]# file test.txt 
test.txt: ASCII text, with CRLF, CR line terminators, with overstriking

文件类型为二进制文件,想了很多办法,尝试使用dos2unix,无果。

[root@Zabbix93 expect]# dos2unix test.txt 
dos2unix: Binary symbol found at line 43
dos2unix: Skipping binary file test.txt

不考虑,直接放弃该方法,转而使用另一种方法:通过tftp上传到Linux的tftp服务器上面。

 1、安装tftp相关软件包

[root@Zabbix93 ~]# 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端口,所以需要在防火墙开放此端口。

扫描二维码关注公众号,回复: 3601014 查看本文章
  service xinetd restart

4、创建/tftpboot,并赋予读写权限。

[root@Zabbix93 /]# chmod 705 tftp/
[root@Zabbix93 /]# 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
[root@Zabbix93 /]# cd /tftp/
[root@Zabbix93 tftp]# ls
config.text 

至此成功备份交换机配置文件到服务器上,只要写一个crontab定时执行就可以定时备份。

猜你喜欢

转载自blog.csdn.net/qq_41571056/article/details/83059373
今日推荐