マルチスレッドラーニング――pythonスクリプトによるファーウェイスイッチポート構成の一括変更
1091 ワード
import telnetlib,threading
ipfile = open('swiplist.txt')
ipall = [ ip.strip('
') for ip in ipfile.readlines() ]
def get_mac(ip):
try:
tel = telnetlib.Telnet(ip)
tel.read_until('Username:')
tel.write('admin'+'
')
tel.read_until('Password:')
tel.write('password'+'
')
tel.read_until('>')
tel.write('dis mac-add ' + mac +'
')
data = tel.read_until('>')
if 'sticky' in data:
print 'telnet '+ ip
print data
print ip,'done'
except:print 'can not connect to %s' %ip
threads = []
numbers = range(len(ipall))
mac = raw_input('input mac add ,like => aabb-ccdd-eeff
>')
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'