__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) + ' process started ')
Num+=1
Pool.apply_async (Telnet, (argu[0],argu[1]))
# multipro.append (t)
# T.start ()
# T.join ()
# print (' +str (num) + ' process started ')
# 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) + ' process started ')
# num+=1
Except Exception as E:
Print (e)
if __name__ = = "__main__":
Multitelnet ()
# telnet ("172.25.11.2", B "mpls-vrf-pe1")
Python implements the concurrency of the process