cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2443
Views
0
Helpful
9
Replies

python automation backup all my routers

R Manjunatha
Level 3
Level 3

Hi

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

My sample router configuration is as follows.

R120

interface Loopback1
ip address 172.120.10.1 255.255.255.0
!
interface Loopback2
ip address 172.120.20.1 255.255.255.0
!
interface Loopback3
ip address 172.120.30.1 255.255.255.0
!
interface Loopback4
ip address 172.120.40.1 255.255.255.0
!
interface Ethernet0/0
ip address 10.10.120.2 255.255.255.0
duplex auto
!
interface Ethernet0/1
ip address dhcp
duplex auto
!
interface Ethernet0/2
no ip address
shutdown
duplex auto
!
interface Ethernet0/3
no ip address
shutdown
duplex auto
!
!
router eigrp 100
network 10.10.120.0 0.0.0.255
network 172.120.10.0 0.0.0.255
network 172.120.20.0 0.0.0.255
network 172.120.30.0 0.0.0.255
network 172.120.40.0 0.0.0.255
network 192.168.1.0
network 192.168.50.0
!
ip forward-protocol nd
!
!
no ip http server
no ip http secure-server
ip route 0.0.0.0 0.0.0.0 192.168.50.1
ip ssh version 2

 

9 Replies 9

Hello,

you could use Paramiko. The link below is a good read:

https://4sysops.com/archives/cisco-config-backup-with-python/

Yes ...I have done the same unable to get a backup configuration from my router R120

in this script, there is no target IP address .
 
# 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()
 
RManjunatha_0-1663507505017.png

after running this scrip there is no error as well

 

Hello,

--> in this script, there is no target IP address

Did configure the --devicelist = "device-list1.txt" ? The text file should contain all of your target IP addresses...

RManjunatha_0-1663565153328.pngRManjunatha_1-1663565202373.png

Hi,

 

I have created device list and its generated one file after running script in that "Line has invalid autocommand "show run" 

Hello,

the script looks by the book. I'll try and find what causes this error...

EDIT: I wonder if the problem is the 'show run' you are trying to execute. Do you get the same error when you run e.g. 'sh clock' ?

RManjunatha_0-1663580659568.png

 

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.

My router login stage is as follows and I believe this script is executed in user mode 

login as: admin
| Password:

R120>en
Password:
R120#

R Manjunatha
Level 3
Level 3

Hi,

 

Please help to resolve this issue ...

Hello,

can you post the full running config (sh run) of R120 ?