码迷,mamicode.com
首页 > 编程语言 > 详细

python实现进程的并发

时间:2016-09-14 16:52:56      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:

__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实现进程的并发

标签:

原文地址:http://www.cnblogs.com/luo-mao/p/5872538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!