简体   繁体   English

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

[英]How to speed up a script to get backups from routers in python

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).我有一个脚本,可以从文本文件中获取 IP,然后获取备份或向路由器或交换机(ASR、Nexus、Catalyst 等)引入命令。 I am using Python version 2.7.12 and telnetlib module.我正在使用 Python 版本 2.7.12 和 telnetlib 模块。 The problem is that i takes 1 hour for just 200 devices, so it is not very efficient.问题是我只需要 200 台设备就需要 1 小时,所以效率不是很高。 Maybe running multiple processes in parellel would be the solution ?也许并行运行多个进程将是解决方案? I attached the snippet.我附上了片段。

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

Well after 1 day of struggle, i did it.经过1天的斗争,我做到了。 Netmiko is a great module but only works for ssh and i need it telnet access, so i stuck with this one. Netmiko 是一个很棒的模块,但只适用于 ssh,我需要它 telnet 访问,所以我坚持使用这个。 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.我通过可以具有不同列数的文件文本获取设备的 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)

exit()出口()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM