cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1732
Views
0
Helpful
8
Replies

python automation for cisco router backup

R Manjunatha
Level 3
Level 3

Hi

I need to schedule in python script automation backup for all my routers, please let me know the script which I can run the same.

My sample script is as follows.

# by John Kull

# import modules needed and set up ssh connection parameters
import paramiko
import datetime
user = 'admin'
secret = 'admin'
port = 22
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# define variables
time_now = datetime.datetime.now().strftime('%m_%d_%Y_%H_%M_%S')
infilepath = "F:\\Automation\\cisco automation\\"
outfilepath = "F:\\Automation\\cisco automation\\"
devicelist = "device-list1.txt"

# open device file
input_file = open( infilepath + devicelist, "r")
iplist = input_file.readlines()
input_file.close()

# loop through device list and execute commands
for ip in iplist:
ipaddr = ip.strip()
ssh.connect(hostname=ipaddr, username=user, password=secret, port=port)
stdin, stdout, stderr = ssh.exec_command('show run')
list = stdout.readlines()
outfile = open(outfilepath + ipaddr + "_" + time_now, "w")
for char in list:
outfile.write(char)
ssh.close()
outfile.close()

 

I have created device list and its generated one file after running script in that "Line has invalid autocommand "show run"
the script in enable mode login to the router, since show run command is not accepting in the router

R43>sh run
^
% Invalid input detected at '^' marker.

8 Replies 8

Hello,

as stated, the script looks good, I don't really know why it does not work. Does the 'admin' user have privilege 15 on the target device(s) ?

As an alternative, you could use the Netmiko script linked below:

https://www.rogerperkin.co.uk/network-automation/python/scripts-for-network-engineers/#3-Python-Script-to-backup-Cisco-config

Hi

yes, I run the script and I am getting an error as shown in the snapshot

balaji.bandi
Hall of Fame
Hall of Fame

R43>sh run 

this error because you have not add enable password to parse in the script.

when you login to device, are you able to get directly to # prompt with admin admin

i used same simple script using paramiko works for me ( but the userr is priv 15 and added vty lines same to get in to # mode)

https://www.balajibandi.com/?p=1740

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Hi,

My backup script is working fine now and what you posted that is having some error

same to get into # mode

PS F:\Automation\cisco automation> ssh -l admin 192.168.50.192
Password:

R120#

I believe i may have tested this 2.7, python 3.10 you running, so the syntax changed

print (output)  or you can hash that Line 43  #print output

 

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Hi,

I am using Python 3.10.8

Yes, print output I have used bracket print (output) no error 

but line 17 its says that no cisco_devices file found, but I created same file and added device Ip address as you mentioned in the same folder

if you have extension .txt change line as below and test it :

f = open('cisco_devices.txt')

I use Linux so my file name is  - cisco_devices

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Okay let me check in Linux and in the older version of python 

Review Cisco Networking for a $25 gift card