經過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)
退出()
我第一次嘗試使用這個庫,稱爲[Netmiko(https://pynet.twb-tech.com/blog/automation/netmiko.html)。如果仍然沒有幫助,那麼[multiprocessing](https://docs.python.org/2/library/multiprocessing.html)就是要走的路。 – sal
謝謝,它使用多處理。 120個設備大約需要3分鐘,所以現在它相當不錯。腳本開始:2016年9月16日16點05分10秒 腳本停止:2016年9月16日16時08分14秒 – jocmtb
這真棒。也許你可以在這裏發佈答案,讓社區受益。 – sal