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")
  • 相关阅读:
    Linux系统安装Apache 2.4.6
    Redhat Server 5.7 安装配置PHP
    ORACLE基本数据类型总结
    Nagios学习实践系列——产品介绍篇
    Linux技术修复
    python的特殊方法:
    python获取对象信息
    python多重继承:
    python多态
    python类的继承
  • 原文地址:https://www.cnblogs.com/luo-mao/p/5872538.html
Copyright © 2011-2022 走看看