How to speed up a script to get backups from routers in python
<p>I have a script that gets IPs from a text file, and then get a backup or introduces commands to routers or switches(ASR,Nexus,Catalyst,etc). I am using Python version 2.7.12 and telnetlib module.
The problem is that  i takes 1 hour for just 200 devices, so it is not very efficient. Maybe running multiple processes in parellel would be the solution ?
I attached the snippet.</p>

<pre><code>#!/usr/bin/python
#-------------------------------------------------------------------------------
# Purpose:     Get Backup
# Enterprise:  CLARO Cisco
# Date:     31 de Agosto
#-------------------------------------------------------------------------------

import datetime
import getpass
import sys
import telnetlib
import os


x = datetime.datetime.now()
#date = ("%s-%s-%s" % (x.year, x.month, x.day) )
date = ("%s-%s-%s_%s:%s" % (x.year, x.month, x.day,x.hour,x.minute) )
HOST = []
file = open('./file.txt','r')
NUM= len(file.readlines())
file.seek(0)

for j in range(0,NUM):
 JOC=file.readline()
 part=JOC.split()
 if len(part)&gt;1:
  HOST.append(part[0].strip())
 else:
  HOST.append(JOC.strip())

file.close()

print "###Script to get backup from Cisco devices####"
print HOST
user = "usr"
password = "pwd"
enable = "nbl"

carpeta = "/home/jocmtb/BACKUP_" + date
os.makedirs(carpeta)

print "###Getting info from devices listed above####"


for item in HOST:
        try:
                rutadir = "./BACKUP_"+date+"/"+date +"_"+ item
                tn = telnetlib.Telnet(item)
                tn.read_until("Username: ")
                tn.write((user + "\n").encode('ascii'))
                tn.read_until("Password: ")
                tn.write((password + "\n").encode('ascii'))
                tn.write("enable\n")
                tn.read_until("Password: ")
                tn.write((enable + "\n").encode('ascii'))
                tn.write("terminal len 0\n")
                tn.write("sh version | i Software\n")
                tn.write("exit\n")
                print "# Getting info from device "+item
                running = tn.read_until("^exit\n")
                FILE = open(rutadir, "w")
                FILE.write(running)
                FILE.close()
                print "# Finish"
                tn.close()
                del tn
        except:
                print("Unexpected error: on host " + item)
exit()
</code></pre>

<p>Well after 1 day of struggle, i did it. Netmiko is a great module but only works for ssh and i need it telnet access, so i stuck with this one.
Here is the final code. Hope someone think is usefull. I get the IPs of the devices via a file text that that can have different numbers of columns.  </p>

<pre><code>from multiprocessing import Pool
import datetime
import getpass
import time
import sys
import telnetlib
import os

def f1():
    print "###Script to get backup from Cisco devices####"
    print HOST
    print "###Getting info from devices listed above####"
    for item in HOST:
            try:
                    rutadir = "./BACKUP_"+date+"/"+date +"_"+ item
                    tn = telnetlib.Telnet(item)
                    tn.read_until("Username: ")
                    tn.write((user + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.read_until("Password: ")
                    tn.write((password + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.write("enable\n")
                    tn.read_until("Password: ")
                    tn.write((enable + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.write("terminal len 0\n")
                    time.sleep(1)
                    tn.write("sh version\n")
                    time.sleep(1)
                    tn.write("exit\n")
                    time.sleep(1)
                    #print "# Getting info from device "+item
                    running = tn.read_until("^exit\n")
                    FILE = open(rutadir, "w")
                    FILE.write(running)
                    FILE.close()
                    print "# Getting info from device "+item+" OK"
                    tn.close()
                    del tn
            except:
                    print("# Unexpected ERROR: on host " + item)

def f2():
    print HOST2
    for item2 in HOST2:
            try:
                    rutadir = "./BACKUP_"+date+"/"+date +"_"+ item2
                    tn = telnetlib.Telnet(item2)
                    tn.read_until("Username: ")
                    tn.write((user2 + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.read_until("Password: ")
                    tn.write((password2 + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.write("terminal len 0\n")
                    tn.write("sh version\n")
                    tn.write("exit\n")
                    #print "# Getting info from device "+item2
                    running = tn.read_until("^exit\n")
                    FILE = open(rutadir, "w")
                    FILE.write(running)
                    FILE.close()
                    print "# Getting info from device "+item2+" OK"
                    tn.close()
                    del tn
            except:
                    print("# Unexpected ERROR: on host " + item2)

def f3():
    print HOST3
    for item3 in HOST3:
            try:
                    rutadir = "./BACKUP_"+date+"/"+date +"_"+ item3
                    tn = telnetlib.Telnet(item3)
                    tn.read_until("login: ")
                    tn.write((user + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.read_until("Password: ")
                    tn.write((password + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.write("terminal len 0\n")
                    time.sleep(1)
                    tn.write("sh version\n")
                    time.sleep(1)
                    tn.write("exit\n")
                    #print "# Getting info from device "+item3
                    running = tn.read_until("^exit\n")
                    FILE = open(rutadir, "w")
                    FILE.write(running)
                    FILE.close()
                    print "# Getting info from device "+item3+" OK"
                    tn.close()
                    del tn
            except:
                    tn.close()
                    FILE.close()
                    del tn
                    print("# Unexpected ERROR: on host " + item3)

def f4():
    print HOST4
    for item4 in HOST4:
            try:
                    rutadir = "./BACKUP_"+date+"/"+date +"_"+ item4
                    tn = telnetlib.Telnet(item4)
                    tn.read_until("Username: ")
                    tn.write((user2 + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.read_until("Password: ")
                    tn.write((password2 + "\n").encode('ascii'))
                    time.sleep(1)
                    tn.write("terminal len 0\n")
                    tn.write("sh version\n")
                    tn.write("exit\n")
                    #print "# Getting info from device "+item4
                    running = tn.read_until("^exit\n")
                    FILE = open(rutadir, "w")
                    FILE.write(running)
                    FILE.close()
                    print "# Getting info from device "+item4+" OK"
                    tn.close()
                    del tn
            except:
                    print("# Unexpected ERROR: on host " + item4)


if __name__ == '__main__':

    x = datetime.datetime.now()
    date = ("%s-%s-%s_%s:%s" % (x.year, x.month, x.day,x.hour,x.minute) )
    START = ("%s-%s-%s_%s:%s:%s" % (x.year, x.month, x.day,x.hour,x.minute,x.second) )
    HOST = []
    HOST2 = []
    HOST3 = []
    HOST4 = []
    file = open('./file.txt','r')
    NUM= len(file.readlines())
    file.seek(0)
    for j in range(0,NUM):
            JOC=file.readline()
            part=JOC.split()
            if len(part)&gt;1:
                    HOST.append(part[0].strip())
            else:
                    HOST.append(JOC.strip())

    file2 = open('./file2.txt','r')
    NUM2= len(file2.readlines())
    file2.seek(0)
    for j in range(0,NUM2):
            JOC2=file2.readline()
            part2=JOC2.split()
            if len(part2)&gt;1:
                    HOST2.append(part2[0].strip())
            else:
                    HOST2.append(JOC2.strip())

    file3 = open('./file3.txt','r')
    NUM3= len(file3.readlines())
    file3.seek(0)
    for j in range(0,NUM3):
            JOC3=file3.readline()
            part3=JOC3.split()
            if len(part3)&gt;1:
                    HOST3.append(part3[0].strip())
            else:
                    HOST3.append(JOC3.strip())

    file4 = open('./file4.txt','r')
    NUM4= len(file4.readlines())
    file4.seek(0)
    for j in range(0,NUM4):
            JOC4=file4.readline()
            part4=JOC4.split()
            if len(part4)&gt;1:
                    HOST4.append(part4[0].strip())
            else:
                    HOST4.append(JOC4.strip())

    file.close()
    file2.close()
    file3.close()
    file4.close()

    user = "user"
    password = "pwd"
    enable = "enable"
    user2 = "user2"
    password2 = "pwd2"
    carpeta = "/home/user/BACKUP_" + date
    os.makedirs(carpeta)

    pool = Pool(processes=4)             # start 4 worker processes
    result1 = pool.apply_async(f1)   # evaluate "f(10)" asynchronously
    result2 = pool.apply_async(f2)
    result3 = pool.apply_async(f3)
    result4 = pool.apply_async(f4)
    pool.close()

    result1.get()
    result2.get()
    result3.get()
    result4.get()

    y = datetime.datetime.now()
    STOP = ("%s-%s-%s_%s:%s:%s" % (y.year, y.month, y.day,y.hour,y.minute,y.second) )
    print("##Time Execution of the script##")
    print("# Time Script start: " + START)
    print("# Time Script stop: " + STOP)
</code></pre>

<p>exit()</p>

python networking backup cisco