Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi thread telnet issue
#1
Below script is working, however randomly few devices showing as time out, but those devices are reachable to able to telnet.

Thanks for our Moderators/admin and members supporting for this script.

#!/usr/bin/python
import threading
import sys
import os
import unidecode
import telnetlib
import time
import re
from time import sleep
start = time.time()
user = 'eciecidslam'
password = 'Hi-FOCuS'
start = time.time()
macfile = open('mumecimac.txt','w')
def open_telnet(host):
#with open('eci.txt','r') as ipfile:
#      for sr_no, line in enumerate(ipfile, start=1):
#        host = line.strip()
   timeout = 120
   sr_no = 0
   try:
        session = telnetlib.Telnet(host, 23, timeout)
        time.sleep(2)
        session.read_until(b"Login :")
        session.write((user+"\r").encode('ascii'))
        time.sleep(2)
        session.read_until(b"Password :",2)
        session.write((password + "\r").encode('ascii'))
        time.sleep(2)
        session.read_until(b"MCR64A >")
        session.write("dishash all".encode('ascii') + b"\r")
        output = session.read_until("MCR64A >".encode('ascii'), timeout )
        for line in output.decode('utf-8').split('\n')[4:]:
           line = line.split('|')
           if len(line) == 8:
              mac, bport, vlan = map(lambda x: x.strip(), line[:3])
              mac_address = mac2norm(mac)
              #MyList = host,mac_address, bport, vlan
              command = "bportr " +bport
              session.write((command +"\r").encode('ascii'))
              portoutput = session.read_until("MCR64A >".encode('ascii'), timeout )
              for line in portoutput.split('\n'):
                  if 'Port:' in line:
                      port = line.replace(' ','').split(':')[1]
                      #MyList += (port,)
                      macfile.write('{} {} {} {} {}\n'.format(host,mac_address, bport, vlan, port))
        session.write(("logout"+"\r").encode('ascii'))
        session.close()
   except Exception as excp:
            print('time out:- {}\n '.format(host))

def mac2norm(mac):
           mac = mac.split(' ')
           return '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac).upper()

def create_threads():
    threads = []
    with open('mumeci.txt','r') as ipfile:
        for sr_no, line in enumerate(ipfile, start=1):
            host = line.strip()
            time.sleep(2)
            th = threading.Thread(target = open_telnet ,args = (host,))
            time.sleep(5)
            th.start()
            threads.append(th)
        for thr in threads:
            thr.join()

if __name__ == "__main__":
        create_threads()
        print ("Exiting the program")
        #outfile.close()
        macfile.close()
        print('It took', time.time()-start, 'seconds.')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Telnet command in Python 3.9 Dre83 0 1,950 Nov-11-2020, 11:42 AM
Last Post: Dre83
  Sending command using telnet mradovan1208 1 3,936 Apr-24-2020, 07:09 PM
Last Post: Larz60+
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,533 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Serial to telnet using Python Charles_Linquist 2 6,186 Jun-17-2019, 04:37 PM
Last Post: DeaD_EyE
  problem to use multi-threads in a code for telnet sessions anna 6 6,056 Jun-27-2018, 10:02 AM
Last Post: anna
  Issue python3.6 while inheriting telnet library sourabhjaiswal92 4 4,077 May-09-2018, 05:20 AM
Last Post: sourabhjaiswal92
  telnet unexpected output printing anna 3 3,570 Jan-17-2018, 01:16 PM
Last Post: anna
  Issue using print statement in a thread bweiss1258 9 5,377 Jan-16-2018, 02:07 AM
Last Post: bweiss1258
  help required to parsing telnet output anna 8 8,522 Jan-12-2018, 11:05 AM
Last Post: anna
  telnet stuck when get Press any key to continue anna 2 4,747 Dec-31-2017, 11:34 AM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020