2016-09-16 54 views
-1

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

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

我第一次尝试使用这个库,称为[Netmiko(https://pynet.twb-tech.com/blog/automation/netmiko.html)。如果仍然没有帮助,那么[multiprocessing](https://docs.python.org/2/library/multiprocessing.html)就是要走的路。 – sal

+0

谢谢,它使用多处理。 120个设备大约需要3分钟,所以现在它相当不错。脚本开始:2016年9月16日16点05分10秒 脚本停止:2016年9月16日16时08分14秒 – jocmtb

+0

这真棒。也许你可以在这里发布答案,让社区受益。 – sal

回答

0

经过1天的斗争,我做到了。 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) 

退出()