Multi-thread learning-python script batch modify port configurations of Huawei Switches
Import telnetlib, threadingipfile = open('swiplist.txt ') ipall = [ip. strip ('\ n') for ip in ipfile. readlines ()] def get_mac (ip): try: tel = telnetlib. telnet (ip) tel. read_until ('username: ') tel. write ('admin' + '\ n') tel. read_until ('password: ') tel. write ('Password' + '\ n') tel. read_until ('>') tel. write ('dis mac-add' + mac + '\ n') data = tel. read_until ('>') if 'sticky' in data: print 'telnet '+ ip print data print ip, 'done' doesn't: print 'can not connect to % s' % ipthreads = [] numbers = range (len (ipall) mac = raw_input ('input mac add, like => aabb-ccdd-eeff \ n> ') for I in numbers: t = threading. thread (target = get_mac, args = (ipall [I],) threads. append (t) if _ name _ = '_ main _': for I in numbers: threads [I]. start () print 'all done'