Python Forum
subprocess in thread python to check reachability
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess in thread python to check reachability
#1
Hi All,

I would like to put subprocess in thread to check reachability in less time. Below is the my code. First its check reachability and put alive hosts in alive list and initiate threads to telnet and capture Serial number and Hardware information. I am implementing to check more than 2500+ devices.


import threading
import unidecode
import telnetlib
import time
import re
import subprocess
alive = []
dead = []
user = 'eci'
password = 'Hi-test123'

def open_telnet(host):
   timeout = 120
   sr_no = 0
   try:
        session = telnetlib.Telnet(host, 23, timeout)
        time.sleep(1)
        #session.set_debuglevel(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("eer".encode('ascii') + b"\r")
        session.write(" ".encode('ascii'))
        session.write(" ".encode('ascii'))
        session.write(" ".encode('ascii'))
        time.sleep(2)
        output = session.read_until("MCR64A >".encode('ascii'), timeout )
        session.write(("logout"+"\r").encode('ascii'))
        session.close()
        #print(output)
        for line in output.split('\n')[1:]:
            #print line
            if 'HW DESCRIPTION' in line:
                hw_description = line.split('||')[1]
            elif 'SERIAL NUMBER' in line:
                sr_number = line.split('||')[1]
                print('{} {} {}'.format(host.strip(),sr_number.strip(),hw_description.strip()))
   except Exception as excp:
            print('time out:- {}\n '.format(host))
def hostdetails():
    with open('eciserial.txt', 'r') as f:
        for ip in f:
            ip.strip()
            result=subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2",    ip],stdout=f, stderr=f).wait()
            if result:
               dead.append(ip)
            else:
               alive.append(ip)

def create_threads():
    threads = []
    for ip in alive:
        th = threading.Thread(target = open_telnet ,args = (ip,))
        th.start()
        time.sleep(0.5)
        threads.append(th)
    for thr in threads:
        thr.join()

if __name__ == "__main__":
        hostdetails()
        create_threads()
        print('Rechable :', len(alive))
        print "Exiting the program"
Reply
#2
What is the problem with the code you posted? Also, you should be able to use the less complicated subprocess' check_ouput() function for the ping results.
Reply
#3
No issue in current code, however posted for improvement/correction for speed up process.

regards
Anna
Reply
#4
Most of the time taken is because of the time.sleep statements.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python loop in subprocess vinothkumargomu 6 3,179 Jul-06-2020, 12:02 PM
Last Post: vinothkumargomu
  how to check for thread kill flag nanok66 1 2,175 May-09-2020, 10:06 PM
Last Post: nanok66
  Is there a way to not terminate a running thread in python Contra_Boy 3 1,974 May-05-2020, 09:38 PM
Last Post: SheeppOSU
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,563 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Python Thread stops execution neethuvp 1 3,403 Feb-18-2019, 06:36 PM
Last Post: micseydel
  Python Error in subprocess.py roydianton90 1 5,590 Dec-14-2018, 11:26 AM
Last Post: jeanMichelBain
  Debugging inside a thread with Python 2.3.4 zerajera 0 1,691 Nov-21-2018, 01:43 PM
Last Post: zerajera
  OS command via python subprocess module alinaveed786 21 10,818 Oct-23-2018, 05:40 AM
Last Post: alinaveed786
  Parallizing subprocess.check Raymond 0 1,825 Oct-20-2018, 04:32 PM
Last Post: Raymond
  Using Subprocess.Popen to start another python script running in background on Window johnb546 0 13,661 Jun-01-2018, 01:57 PM
Last Post: johnb546

Forum Jump:

User Panel Messages

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