Real-time automatic backup of switch configuration files every day with Linux server

  
        

As a network operation and maintenance engineer, if you manage more routing and switching devices, then will you encounter the same problem as me: device configuration is often adjusted, especially the access layer switch, in case I forgot to save the configuration someday, and the next day, the switch was powered down and restarted, but you forgot the previous configuration, resulting in a long time for network recovery configuration.

When you encounter such problems or troubles, do you also think about it: If there is a way, let the switch automatically save the configuration every day. You should consider further, if the switch can not only save the configuration itself, but also upload the configuration to the server, it would be great.


If you look carefully at the following, you may be inspired.


In today's highly intelligent world, many switches have been able to perform automatic save configuration every day. The following is an example of Huawei. [switch]set save-configuration backup-to-server server 192.168.1.1 transport-type ftp user admin password admin123 path /configbck

//set save-configuration Backup to server server IP is 192.168.1.1, backup The mode is FTP, the username is admin password is admin123 backup path is /configbck//

[switch]set save-configuration delay 5 //5 minutes to execute the auto save configuration //

[switch]set save-configuration internal 43200 //Cycle every 12 hours //

[switch]dis saved-configuration time //Can see the way to save the configuration before //

The following picture shows the auto-saved configuration file I saw on the FTP server.


However, the switch is automatically executed, not supported by all versions, nor supported by all brands. If the low version is not supported, it can be achieved by upgrading the system version of the switch. However, upgrading the switch system requires network disconnection and is also risky.


Below, I posted a small script I wrote to implement this backup using Linux. The function of this small script is to replace the manual configuration on the switch. It is a complete operation of logging in to the switch. Regardless of the version of the switch, as long as the command can be played within the permission, it can be basically realized.

The execution process of the script is like this: telnet to the specified switch - save the configuration - log in to the server through the switch - enter the username and password - upload the configuration file of the switch to the FTP server.

The following is the script content (vi tel104.exp):

#!/usr/bin/expect //This is expected. If linux does not exist, you need to install yum //set date [exec date "+%Y%m%d"] //Assign a value to the date variable, you need to call //spawn telnet 192.168.1.104 //This is a command of expect, which means telnet from the server to 192.168 .1.104//expect “Username:" //matches Username: in the echoed string. If it matches, execute ////send "admin\ " //send admin string to the switch, ie input Telnet to the switch username //expect "Password:" //match Password in the echoed string: if it matches, execute ///send "Admin@huawei\ " //send to the switch Admin@huawei string. That is, enter the password of tel to the switch //

send "save\ " //send the save string to the switch, which is equivalent to executing the save command on the switch //send "Y\ " //Because the switch is in the save state, there will be an interaction process, sending the string Y, which is equivalent to inputting Y//

send "ftp 192.168.1.1\ " on the switch. //Send ftp 192.168.1.1 to the switch. The command is equivalent to inputting the ftp 192.168.1.1 command on the switch, meaning to log in to the FTP server //send "admin\ " //send the string admin, that is, enter the username to log in to the FTP server //send " Admin@huawei\ " //Send the string Admin@huawei. That is, enter the password to log in to the FTP server //send "put flash:/vrpcfg.zip /configbck/$date.zip\ " //Execute the command Put, and upload the configuration file to the FTP server //interact ~

Description: 1. On the Linux server, you must first install expect, otherwise you cannot execute the expect command.

2, Linux server to install telnet, otherwise it can not telnet. Installation can be done with yum.

3, what instructions are sent to the switch, different switches are not the same, according to the need to select the string to be sent, that is, according to the need to specify the command you want to enter in the switch. Of course, not only backup, but also reload /switchport mode trunk can do.

4, As for how to set up an FTP server, how to use the depth more deeply, please Baidu or google.


The next thing we have to do is create a crontab auto-execute task

[root@permanet ~]# crontab -e

00 01 * * * /home/expect/tel104.exp //1:0, daily per week, execute the command under absolute path //15 01 * * * /home/expect/tel105.exp //1 point 15 Points, daily, weekly, weekly, execute the command under the absolute path //

30 01 * * * /home/expect/tel105.exp //1:30, daily and monthly, Execute the command under the absolute path //

45 01 * * * /home/expect/tel106.exp //1:45, daily per week, execute the command under the absolute path //< Br>

55 01 * * * /home/expect/tel107.exp //1:55, daily per week, execute the command under absolute path //

Start at 1am Automatically go to a specified device to save the configuration file to the server every 15 minutes.

Copyright © Windows knowledge All Rights Reserved