|
| 1 | +#!/usr/bin/env python |
| 2 | +#THIS IS A SIMPLE PYTHON SCRIPT FOR BACKING UP CISCO IOS ROUTER CONFIG |
| 3 | +#This type of script is mainly executable in Debain based Linux. |
| 4 | +#Install Paramiko module using "pip" |
| 5 | +#Also, install TFTP server on debian. |
| 6 | +#I have used GNS3 for emulating the router.... |
| 7 | +# make this script executable by using "chmod +x router_script.py" |
| 8 | + |
| 9 | +#@@@@@@@@@@ WARNING: Please review the script and setup the parameters accordingly @@@@@@@@ |
| 10 | +#@@@@@@@@@@ BEFORE DEPLOYING into PRODUCTION NETWORKS. @@@@@@@@ |
| 11 | + |
| 12 | +import paramiko #importing this module for making SSH connection into device |
| 13 | +import sys |
| 14 | +import re |
| 15 | +import time |
| 16 | +import getpass # masking the password |
| 17 | + |
| 18 | +def open_ssh_conn(ip): |
| 19 | + # Try-except block in order to catch the exception of wrong username and password |
| 20 | + try: |
| 21 | + #ask for username, you don't want username to be default |
| 22 | + username = raw_input("Username: ") |
| 23 | + |
| 24 | + #ask for password, and mask it |
| 25 | + |
| 26 | + password = getpass.getpass() |
| 27 | + |
| 28 | + |
| 29 | + #open session using Paramiko |
| 30 | + session = paramiko.SSHClient() |
| 31 | + |
| 32 | + #allow auto-add user policy even if no host keys are configured/implemented |
| 33 | + |
| 34 | + session.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 35 | + |
| 36 | + #passing the necessary paramaters |
| 37 | + |
| 38 | + session.connect(ip, port=22, username = username, password = password) |
| 39 | + |
| 40 | + #start the shell |
| 41 | + |
| 42 | + connection = session.invoke_shell() |
| 43 | + output = connection.recv(65535) |
| 44 | + |
| 45 | + #TFTP Server |
| 46 | + tftp_server = raw_input("IP address of TFTP-server? ") |
| 47 | + #tftp_server = "X.X.X.X" #Directly state your own TFTP server IP instead of asking repeatedly |
| 48 | + |
| 49 | + #Ask Destination file |
| 50 | + dest_file = raw_input("Destination file name? ") |
| 51 | + time.sleep(1) |
| 52 | + #enter into privelege mode from user mode |
| 53 | + connection.send("enable") |
| 54 | + connection.send("\n") |
| 55 | + time.sleep(1) |
| 56 | + #Execute command |
| 57 | + |
| 58 | + connection.send("copy startup-config tftp://"+str(tftp_server)) |
| 59 | + connection.send("\n") #sending above command |
| 60 | + |
| 61 | + |
| 62 | + output += connection.recv(65535) |
| 63 | + print ''.join(output) |
| 64 | + time.sleep(1) |
| 65 | + output += connection.recv(65535) |
| 66 | + print "".join(output) |
| 67 | + connection.send("\n") #sending the confirmation from router for (Remote address []?) |
| 68 | + time.sleep(1) |
| 69 | + output += connection.recv(65535) |
| 70 | + print "".join(output) |
| 71 | + connection.send(dest_file) |
| 72 | + time.sleep(1) |
| 73 | + |
| 74 | + connection.send("\n") |
| 75 | + |
| 76 | + |
| 77 | + time.sleep(1) |
| 78 | + output += connection.recv(65535) |
| 79 | + print "".join(output) |
| 80 | + session.close() |
| 81 | + time.sleep(1) |
| 82 | + |
| 83 | + print "Backup sucessful....Exiting...Please wait" |
| 84 | + |
| 85 | + except paramiko.AuthenticationException: |
| 86 | + print "Invalid Username or password" |
| 87 | + print "Closing program...\n" |
| 88 | + |
| 89 | +#calling SSH Function |
| 90 | +print("########################################################################") |
| 91 | +print("####################### ROUTER BACKUP SCRIPT ###########################") |
| 92 | +print("########################################################################") |
| 93 | + |
| 94 | +IP_address = raw_input("Enter the IP address of the device: ") |
| 95 | +open_ssh_conn(IP_address) |
| 96 | + |
0 commit comments