Subprocesses on Windows

Nachiket Joshi nachieket at yahoo.com
Tue Oct 2 11:05:51 EDT 2007


Hey Guys!

    Well before I explain my problem, let me tell you all that I am not a professional programmer and new to python too. I just write some scripts as and when required and this time it seems I am stuck somewhere in trying to create subprocesses.

    The problem is something like this. I am trying to run nmap scan on my network and for that I want to start multiple nmap instances (256 at once) togather and wait till those 256 are over before I start next lot of 256 instances. Well to test I have put in two nmap scans in my program and I have succeeded to start both of them simultaneously (in fact one after another) by using "subprocess module and Popen".  But the problem is when I run a loop to start both of these nmap instances, they do start BUT the script then exits to command prompt completing all the executions. But this is not what I want. I want to wait till both of these nmap processes are finished and then only exit from script (in fact exit from that function or loop). Then only I would be able to call next lot of IP Ranges to scan. I did try os.waitpid() but it gives following error while running the program:

###########################################
[10172, 'None']                       <<< This is the variable where I am storing process IDs
###########################################
Traceback (most recent call last):
  File "C:\python\inscan.py", line 121, in <module>
    clas_b()
  File "C:\python\inscan.py", line 92, in clas_b
    wpid, sts = os.waitpid(retval[i], 0)            <<< and passing here to os.waitpid
OSError: [Errno 10] No child processes
 

    I hope you guys understood! I am pasting my function here. Just have a look and suggest something for that. I heard fork() is not supported for windows and that's why I used 'subprocess module'. 

def clas_b():
    i = 0
    IP = ((10,50,89,0,24), (10,50,90,0,24))
    retval = ['None', 'None']
        
    for k in IP:
        b1 = k[0]; b2 = k[1]; b3 = k[2]
        b4 = k[3]; mkb = k[4]
        
        # Call the subprocess using convenience method
        #retval[i] = subprocess.call(clas_c(b1, b2, b3, b4, mkb), 0, None, None, \
        #                         outptr[i], errptr[i], shell=True)
        
         # clas_c actually runs Nmap command and stores results in an output file
         retval[i] = int(subprocess.Popen(clas_c(b1, b2, b3, b4, mkb), \
                                     shell=True).pid)
        print "###########################################"
        print retval
        print "###########################################"
        
     while 1:
            wpid, sts = os.waitpid(retval[i], 0)
            if os.WIFSTOPPED(sts):
                continue                                                       <<< I don't know anything about this
            elif os.WIFSIGNALED(sts):                            <<< I just found this somewhere and
                return -WTERMSIG(sts)                             <<< pasted to my program for to test
            elif os.WIFEXITED(sts):
                return os.WEXITSTATUS(sts)
            else:
                raise error, "Not stopped, signaled or exited???"
 
        i += 1

Thanks,
Nachiket Joshi


       
____________________________________________________________________________________
Yahoo! oneSearch: Finally, mobile search 
that gives answers, not web links. 
http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071002/b6a73875/attachment.html>


More information about the Python-list mailing list