![]() |
Multithread telnet not working Parallel - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Multithread telnet not working Parallel (/thread-7978.html) |
Multithread telnet not working Parallel - anna - Feb-01-2018 #!/usr/bin/python import threading import sys import os import unidecode import telnetlib import time import re from time import sleep 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:] outfile.write(ip+' '+port3+' '+port_mac) outfile.write('\n') except StopIteration: pass except Exception as excp: print(excp) 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()above script is working sequentially and not Parallel. please suggest. RE: Multithread telnet not working Parallel - ka06059 - Feb-04-2018 what if u indent lines 52 & 53 ... 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: #indent this line thr.join() #indent this linethe code likely started & joined each thread simultanously, could be the main reason why scripts works sequentially RE: Multithread telnet not working Parallel - anna - Feb-05-2018 Thanks, now one small issue, in mutli thread.. lines are being printed as below, 10.217.128.69 5 2 2000 f8:e9:03:80:3a:6d^M10.217.128.127 5 1 3607 e8:cc:18:b1:4b:89 10.217.128.127 11 2 3607 e8:cc:18:60:a7:53 10.217.128.127 12 3 3607 e4:6f:13:86:7c:41 10.217.128.127 25 4 3607 e4:6f:13:86:7a:e8 10.217.128.127 30 5 3607 3c:1e:04:33:87:55 10.217.128.127 39 6 3607 28:28:5d:ed:c2:aa^M10.217.128.74 16 3 3740 00:17:7c:7c:a0:9a I am expecting like below 10.217.128.69 5 2 2000 f8:e9:03:80:3a:6d 10.217.128.127 5 1 3607 e8:cc:18:b1:4b:89 10.217.128.127 11 2 3607 e8:cc:18:60:a7:53 10.217.128.127 12 3 3607 e4:6f:13:86:7c:41 10.217.128.127 25 4 3607 e4:6f:13:86:7a:e8 10.217.128.127 30 5 3607 3c:1e:04:33:87:55 10.217.128.127 39 6 3607 28:28:5d:ed:c2:aa 10.217.128.74 16 3 3740 00:17:7c:7c:a0:9a RE: Multithread telnet not working Parallel - ka06059 - Feb-05-2018 tbh i dont have any idea what telnet is all about, my best guess is '^M' indicates more than 1 ports 'arrived' at the same time ,sort of , so their results printed at the same line try change line 38 to outfile.write(ip+' '+port3+' '+port_mac+'\n') you should get exact outcome in your 'zyxelmac.txt', not in console/ python shell where few spaces maybe printed inconsistantly between printed results because of '\n' RE: Multithread telnet not working Parallel - anna - Feb-05-2018 thanks, will check and update you. Convert expect script to python.. help required - anna - Feb-05-2018 Hi All, I have below script to telnet devices and gather MAC details with noappend mode and storing in file. I would like to convert this to python script. ########## #log_user 0 set timeout 10 set f [open "UT-ROM-Batch-1.txt"] set hosts [split [read -nonewline $f] "\n"] log_file foreach host $hosts { spawn telnet $host expect { "Login:" { } timeout { continue } } send "admin\r" expect "Password:" send "1234\r" expect "Alfran1>" send "dsl -c\r" # Prompt change expect "\\$" # New prompt log_file -noappend /home/anna/scripts/ut/$host.umac send "get bridge port forwarding\r" expect "\\$" send "\x1A" # Send control+z to come out from inner prompt expect "Alfran1>" # Outer prompt send "exit\r" log_file } expect eof close $f RE: Convert expect script to python.. help required - DeaD_EyE - Feb-05-2018 You should take a look into docs.python.org/3.6/library/telnetlib.html. And crosspost: https://python-forum.io/Thread-Multithread-telnet-not-working-Parallel RE: Multithread telnet not working Parallel - anna - Feb-05-2018 import telnetlib import re from time import sleep user = 'admin' password = '1234' ip = '10.19.1.49' telnet = telnetlib.Telnet(ip, 23, 2) telnet.read_until('Login:', 3) telnet.write(user.encode('ascii') + '\r') telnet.read_until('Password:', 3) telnet.write(password.encode('ascii') + '\r') telnet.expect([r"\w+ >$"],2) telnet.write('dsl -c' + '\r') telnet.expect([r'\\$'],2) telnet.write('get bridge port forwarding' +'\r') telnet.read_until('$') telnet.write('\x1A') telnet.expect([r"\w+ >$"],2) telnet.write(' exit'+'\r') output = telnet.read_all() sleep(2) print(output)tried about script but not able to print output. Regex for inner prompt - telnet.expect([r'\\$'],2) Control character from Control+z -is '\x1A', suggest.. how i can trouble shoot this? |