Python – Script8: Take Backup Configs of Multiple devices via SSH (Paramiko)

Step1: Configure file with all IP details of multiple devices

              nano myswitches

192.168.10.21
192.168.10.22
192.168.10.23
192.168.10.24

Step2: Configure file with for python script

              nano switchloopfileparamiko.py

import paramiko
import time
import getpass
 
# Get Username & Password
username = raw_input(“Enter your username: “)
password = getpass.getpass()
 
# Open file with list of switches
f = open (‘myswitches’)
for line in f:
 
    ip_address = line.strip()
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip_address,username=username,password=password)
 
    print “Successful connection”, ip_address
 
    remote_connection = ssh_client.invoke_shell()
    print “Getting running-config of ” + ip_address
 
    remote_connection.send(“enable\n”)
    remote_connection.send(“cisco\n”)
    remote_connection.send(“terminal length 0\n”)
    remote_connection.send(“show run\n”)
    remote_connection.send(“exit\n”)
 
    time.sleep(20)
    readoutput = remote_connection.recv(655350)
    saveoutput = open(“Backup_Switch_” + ip_address, “w”)
    print “Saving Configuration as Backup_Switch_” + ip_address + “\n”
    saveoutput.write(readoutput)
    saveoutput.write(“\n”)
    saveoutput.close

Network Architect | CCIEx3 #29824 JNCIE #2197 VCIX-NV

Leave a Comment