首页 > 代码库 > python实现进程的并发
python实现进程的并发
__author__ = ‘caohuan‘
import telnetlib
import multiprocessing
import random
def telnet(ip,hostname):
tn = telnetlib.Telnet(ip)
print("begin telnet "+str(hostname)+"...")
tn.read_until("Username:".encode(‘ascii‘))
tn.write("nb".encode(‘ascii‘)+b"\n")
tn.read_until(b"Password:")
tn.write("nb".encode(‘ascii‘) + b"\n")
print(hostname.encode(‘ascii‘))
tn.read_until(hostname.encode(‘ascii‘)+ b">")
tn.write("enable".encode(‘ascii‘) + b"\n")
tn.read_until("Password:".encode(‘ascii‘))
tn.write("nb".encode(‘ascii‘) + b"\n")
tn.read_until(hostname.encode(‘ascii‘)+b‘#‘)
tn.write("terminal length 0".encode(‘ascii‘) + b"\n")
tn.read_until(hostname.encode(‘ascii‘)+b‘#‘)
tn.write("show run".encode(‘ascii‘) + b"\n")
aa = tn.read_until(hostname.encode(‘ascii‘)+b‘#‘)
#print(aa.decode(‘ascii‘))
aa1="C:\\tt\\"+hostname+".txt"
# with open("C:\\luo.txt",‘a+‘) as ff:
with open(aa1,‘a+‘) as ff:
ff.write(aa.decode(‘ascii‘))
ff.write("exit%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
tn.write(b"exit\n")
#print("exit")
def multitelnet():
try:
pool=multiprocessing.Pool(processes=4)
f=open("C:\\12345.txt","r")
num=1
multipro=[]
while True:
line=f.readline().strip(‘\n‘)
if line:
argu=line.split(‘\t‘)
#print (argu[0])
#print (argu[1])
print( ‘第‘+str(num)+‘个进程已启动‘)
num+=1
pool.apply_async(telnet,(argu[0],argu[1]))
# multipro.append(t)
# t.start()
# t.join()
# print( ‘第‘+str(num)+‘个进程已启动‘)
# num+=1
else:
break
f.close()
pool.close()
pool.join()
print ("Sub-process(es) done")
# for j in multipro:
# j.start()
# j.join()
# print( ‘第‘+str(num)+‘个进程已启动‘)
# num+=1
except Exception as e:
print (e)
if __name__ == "__main__" :
multitelnet()
# telnet("172.25.11.2",b"mpls-vrf-PE1")
python实现进程的并发
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。