10-17-2022 07:15 AM
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.
10-17-2022 09:19 AM
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:
10-21-2022 08:54 AM
10-21-2022 09:11 AM
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)
10-22-2022 09:08 AM
10-22-2022 11:48 AM
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
10-23-2022 02:33 AM
10-23-2022 03:25 AM
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
10-26-2022 07:10 AM
Okay let me check in Linux and in the older version of python
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide