why and how to run forever and debug when error in for proc in psutil.process_iter()?

Ho Yeung Lee jobmattcon at gmail.com
Sat Mar 25 04:37:54 EDT 2017


expect below to run forever and keep running a fixed number of thread in python

would like to kill tasks when process connect internet except chrome
and explorer.exe

i do this because MalwareBytes can not disconnect these existing trojan when my notebook connect internet

after run a few minutes, the program stopped, but i have already kept create process, why the whole program end?

why and how to debug when error in for proc in psutil.process_iter()?


import os
import psutil
import multiprocessing
import time
import sys

def cleantask():
    p = os.popen("netstat -ano")
    while 1:
        line = p.readline()
        if "TCP" in line or "UDP" in line:
            linelist = line.split()
            if len(linelist) > 4:
                if "LISTEN" in str(linelist[3]):
                    for proc in psutil.process_iter():
                        try:
                            if "pop" not in str(proc.name).tolower():
                                os.system("taskkill /f /pid "+str(proc._pid))
                        except:
                            dummy = 1
                            #print "Unexpected error:", sys.exc_info()[0]
                            #print "Unexpected error:", sys.exc_info()[1]
                if "ESTABLISHED" in str(linelist[3]):
                    if "127.0.0.1" not in str(linelist[2]):
                        for proc in psutil.process_iter():
                            try:
                                if str(linelist[4]) in str(proc._pid):            
                                    print(str(linelist[2])+","+str(linelist[4])+","+proc.name)
                                if "111.221" not in str(linelist[2]) and "explorer.exe" not in str(proc.name).tolower():
                                    os.system("taskkill /f /pid "+str(proc._pid))
                            except:
                                dummy = 1
                                #print "Unexpected error:", sys.exc_info()[0]
                                #print "Unexpected error:", sys.exc_info()[1]
                        print(line)
        if not line: break

if __name__ == '__main__':
    print("main")
    try:
        numberofrunning = 0
        plist = []
        for ii in range(0,5):  
            p = multiprocessing.Process(target=cleantask(), args=(0,))
            p.start()
            plist.append(p)
            numberofrunning = numberofrunning + 1
            time.sleep(1)
        for pp in plist:
            pp.join()
     	    if pp.is_alive() == False:
                numberofrunning = numberofrunning - 1
                plist.remove(pp)
            if numberofrunning > 10:
                print "more than 10 process"
            else:
                print("number of process = " + str(numberofrunning))
                if numberofrunning <= 5:
                    p = multiprocessing.Process(target=cleantask(), args=(0,))
                    p.start()
                    plist.append(p)
                    numberofrunning = numberofrunning + 1
                    time.sleep(1)
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print "Unexpected error:", sys.exc_info()[1]



More information about the Python-list mailing list