Feb-09-2018, 03:26 AM
Hi All,
this code is taking, device ips from file and threads to print output in file. I have replaced multiple spaces with single space. This code is taking around 5min and devices are 450. Is there any tweak in this for faster execution. thanks forum to help me for initial code.
this code is taking, device ips from file and threads to print output in file. I have replaced multiple spaces with single space. This code is taking around 5min and devices are 450. Is there any tweak in this for faster execution. thanks forum to help me for initial code.
import os import unidecode import telnetlib import time import re from time import sleep start = time.time() outfile = open('zyxelmac.txt','w') def open_telnet(ip): user = 'admin' password = '1234' sr_no = 0 try: telnet = telnetlib.Telnet(ip, 23, 2) telnet.read_until('User name:', 3) telnet.write(user.encode('ascii') + '\r') telnet.read_until('Password:', 3) telnet.write(password.encode('ascii') + '\r') telnet.write('statistics mac 1~48' + '\r\r\r\r') telnet.read_until('>') telnet.write(' exit''\r') output = telnet.read_all() sleep(2) data = iter(output.split('\n')) try: for line in data: if "Port:" in line: port1 = line.split(':') port = str(port1[:5]) port2 = port[10:13].strip() port3 = port2.replace('\\','') next(data) next(data) port_mac = next(data)[1:] formatted_line = (ip+' '+port3+' '+port_mac) line = re.sub('\s{2,}', ' ', formatted_line) outfile.write(line+'\n') except StopIteration: pass except Exception as excp: print('time out \n') def create_threads(): threads = [] with open('zyxel.txt','r') as ipfile: for sr_no, line in enumerate(ipfile, start=1): ip = line.strip() th = threading.Thread(target = open_telnet ,args = (ip,)) th.start() threads.append(th) for thr in threads: thr.join() if __name__ == "__main__": create_threads() print "Exiting the program" outfile.close() print('It took', time.time()-start, 'seconds.')