liuxiaoyun111 发表于 2018-7-27 08:03:43

多线程学习——python脚本批量修改华为交换机端口配置

importtelnetlib,threading  
ipfile = 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'
  
    except:print 'can not connect to %s' %ip
  
threads = []
  
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,))
  
    threads.append(t)
  

  
if __name__ == '__main__':
  
    for i in numbers:
  
      threads.start()
  

  
print 'all done'
页: [1]
查看完整版本: 多线程学习——python脚本批量修改华为交换机端口配置