This is the case with the switch as an example of the script written, other manufacturers of equipment according to the device command to modify the script.
Demand
Bulk Backup network device profile to FTP server, and automatically compress the directory where the configuration files are sent to the personal mailbox as an attachment
Environment
Linux Systems: Centos 6.6
Network equipment: CE58/68 switch
IPList file Format: 192.168.1.1:ce6851
192.168.1.2:ce5855
The script reads as follows:
#!/bin/bash
For IP in ' cat/app/backup/iplist '//Read IPList file through for loop
Do
Address= ' echo $IP |awk-f ': ' {print '} '//define variable IP address, use the awk command to ":" To take an IP address for the split symbol
Name= ' echo $IP |awk-f ': ' {print $} '//define variable switch system name
/usr/bin/expect << EOF//Use the expect command to complete interaction with the switch
Spawn ssh-c des-q-o stricthostkeychecking=no [email protected]$address //Start an SSH session
Set Timeout 5
Expect "*password:"
Send "[Email protected]\r"
Expect "*]:"
Send "n\r"
Expect "*>"
Send "FTP ftp_server address\ r"
Expect "none)):"
Send "ftpuser\r"
Expect "*password:*"
Send "[Email protected]\r"
Expect "*ftp]"
Sleep 1
Send "binary\r"
Sleep 1
Expect "*ftp]"
Sleep 1
Send "put vrpcfg.zip ${name}.zip\ r"//modify file name uploaded to FTP server
Sleep 1
Expect "*ftp]"
Sleep 1
Send "quit\r"
Sleep 1
Expect "*>"
Sleep 1
Send "quit\r"
Expect EOF
Eof
Done
time=$ (date "+%y-%m-%d")//define time variable
/usr/bin/zip-r/app/${time}_config.zip/app/ftp/*.zip//Compress the directory where the configuration files are saved on the FTP server
echo "This is Test" | Mail-s "Networkconfig"-a/app/${time}_config.zip ****** @qq. com//through the Mail command to send mail, echo input message content,-s option to configure the message name, the-a option to create an attachment directory
The above is all script content, you can also add the RM command at the end of the script to delete the local profile of the sent message, and then execute the script during the configuration scheduled task cycle.
This script can back up all network device configuration files or issued commands, only need to modify the contents of the expect according to the device.
Network device automatically backs up shell scripts