Start a Conversation

Unsolved

R

7 Posts

1113

October 29th, 2021 13:00

Backup Script for Switches. Tested with N2048 and N3024

Hi!

I would like to share a Script that I made thanks to others script from Internet, a little logic, research and testing. 

You will need installing Python 2.7 and you have to use Telnet in the daily admin. If your switches has ssh instead of telnet, well, You will have to modify all the script.

First, Install Python 2.7. Then, Copy the script in the same server where python is installed. Last, create a .bat and, in Windows task Scheduler, create a task pointing to this .bat. Here is an example of my bat:
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch0.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch1.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch2.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch3.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch4.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch5.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch6.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch7.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch8.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch9.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch10.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch11.py
C:\Python27\python.exe C:\Python27\bkpswitches\Backup_switch12.py

Each script is a copy of the template with the information of each switch.

The template has comments. If you have problems running the script, remove the comments.

Copy and save it as .py extention.

Hope it helps.

 

import telnetlib
import os
import re
import time
import string
import sys
import datetime
#st = datetime.datetime.now().strftime("%A %d de %B del %Y")
st = datetime.datetime.now().strftime("%d-%B-%Y")
HOST = '192.168.0.1' #Type IP Switch
PORT = 23
LOGIN_STRING = "Login:"
PASSWORD_STRING = "Password:"
TERMINAL_LEN_ZERO = "terminal length 0\n"
TERMINAL_MONITOR = "terminal monitor\n"
ENABLE_STRING = "enable\n"
CONFIG_STRING = "configure\n"
USERNAME = 'administrator' #Replace with your adminisitrator user
PASSWORD = 'password' #Replace with your administrator password
ENABLE_PASSWORD = '' #Type Enable Password if exist
TIMEOUT = 3
def do_terminal_settings(tn):
tn.write(TERMINAL_MONITOR)
tn.read_until("#")
tn.write(TERMINAL_LEN_ZERO)
tn.read_until("#")
def do_login(tn):
tn.read_until(LOGIN_STRING, TIMEOUT)
tn.write(USERNAME + "\n")
tn.read_until(PASSWORD_STRING, TIMEOUT)
tn.write(PASSWORD + "\n")
tn.read_until(">", TIMEOUT)
tn.write(ENABLE_STRING)
tn.write(ENABLE_PASSWORD + "\n")
tn.read_until("#", TIMEOUT)
def do_config(tn):
tn.read_until("#", TIMEOUT)
tn.write("copy running-config tftp://192.168.0.9/Switches-Backup/filename_"+st+"\n"); #Replace IP with your TFTP server and folders. Replace filename with one of your choice, but dont delete strings after slash.
tn.read_until("(y/n)", TIMEOUT)
tn.write("y");
tn.read_until("#", TIMEOUT)
def main():
telnet = telnetlib.Telnet(HOST,PORT)
do_login(telnet)
do_terminal_settings(telnet)
do_config(telnet)
telnet.close()
sys.exit(0)
main()

 

 

No Responses!
No Events found!

Top