如何加速脚本从python中的路由器获取备份

时间:2016-09-16 02:33:39

标签: python networking backup cisco

我有一个脚本从文本文件中获取IP,然后获得备份或向路由器或交换机(ASR,Nexus,Catalyst等)引入命令。我使用的是Python 2.7.12版和telnetlib模块。 问题是我只用了200个设备需要1小时,所以效率不高。也许在parellel中运行多个进程会是解决方案吗? 我附上了片段。

#!/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)>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()

1 个答案:

答案 0 :(得分:0)

经过一天的奋斗,我做到了。 Netmiko是一个很棒的模块,但只适用于ssh,我需要telnet访问,所以我坚持这个。 这是最终的代码。希望有人认为是有用的。我通过可以具有不同列数的文件文本获取设备的IP。

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)>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)>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)>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)>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)

出口()