zoukankan      html  css  js  c++  java
  • python实现进程的并发

    __author__ = 'luozt'
    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"
    ")
    
        tn.read_until(b"Password:")
        tn.write("nb".encode('ascii') + b"
    ")
    
        print(hostname.encode('ascii'))
    
        tn.read_until(hostname.encode('ascii')+ b">")
    
    
        tn.write("enable".encode('ascii') + b"
    ")
    
        tn.read_until("Password:".encode('ascii'))
        tn.write("nb".encode('ascii') + b"
    ")
    
        tn.read_until(hostname.encode('ascii')+b'#')
    
        tn.write("terminal length 0".encode('ascii') + b"
    ")
        tn.read_until(hostname.encode('ascii')+b'#')
        tn.write("show run".encode('ascii') + b"
    ")
        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
    ")
        #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('
    ')
            if line:
                argu=line.split('	')
                #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")
  • 相关阅读:
    学习些新东西
    浏览器内的web开发工具
    基于oracle开发的初步接触
    LAMP3 PHP安装
    svn for windows
    PHP替换掉字符串中的非字符
    搭个邮件服务器
    centos下安装mysql
    安装tomcat
    c#线程
  • 原文地址:https://www.cnblogs.com/luo-mao/p/5872538.html
Copyright © 2011-2022 走看看