1

Is it possible to have a script iterate through each IP and backup running configuration on a local tftp server

   import paramiko
    import sys
    import time


    USER = "root"
    PASS = "cisco"
    HOST = ["10.10.10.10","11.11.11.11","12.12.12.12"]
    i=0
    while i <len(HOST)
    def fn():
      client1=paramiko.SSHClient()
      #Add missing client key
      client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      #connect to switch
      client1.connect(HOST,username=USER,password=PASS)
      print "SSH connection to %s established" %HOST

     show run | redirect tftp://10.10.10.20/HOST.cfg 
    print "Configuration has been backed up"for  %HOST
    i+1

show run | redirect tftp://10.10.10.20/HOST.cfg --- can I use variable name as a text file name?

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • What is `redirect`? Is that a command? Where do you want to execute that? Locally or on the routers? – Martin Prikryl Nov 10 '18 at 08:04
  • Redirect is Cisco published command to run on any switch/router to push a running configuration via tftp server running on a local PC. If you know a better way please share. – Jim Halpert Nov 12 '18 at 14:52

1 Answers1

0
for h in HOST:
    client = paramiko.SSHClient()
    #Add missing client key
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    #connect to switch
    client.connect(h, username = USER, password = PASS)
    print("SSH connection to {0} established".format(h))
    command = "show run | redirect tftp://10.10.10.20/{0}.cfg".format(h)
    (stdin, stdout, stderr) = client.exec_command(command)
    for line in stdout.readlines():
        print(line)
    client.close()
    print("Configuration has been backed up for {0}".format(h))

Obligatory warning: Do not use AutoAddPolicy - You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846