简体   繁体   中英

run multiple .exe files at once python

How do you run multiple .exe files at once in Python? I adapted code from another stack overflow question to make a LAN pinger. This pinger has to ping all devices within the subnet mask so in my case it has to run ping.exe 255 times. As a result, it takes a long time to run this. How can I run ping.exe multiple times at once?

The code I am currently using is as follows:

import subprocess
import os
with open(os.devnull, "wb") as limbo:
        print "SCANNING YOUR LAN..."
        for n in xrange(1, 256):
                ip="192.168.0.{0}".format(n)
                result=subprocess.Popen(["ping", "-n", "1", "-w", "200", ip],
                        stdout=limbo, stderr=limbo).wait()
                if result:
                        pass
                else:
                        print ip, "is active"

How do I make this program more efficient?

Don't put wait in your Popen call.

If you need them to run in parallel, but want to wait till they're all done before proceeding, do:

# Create a list of the running processes
running = [subprocess.Popen(...) for ip in ips]
# Wait /after/ all process have launched. 
[process.wait() for process in running]
# Rest of code here.

Of course you have to reformulate things a little to make the ips list, etc.

Well if you had a file called "ipaddress.txt" with all the IPs you could do something like I imagine:

    f = open('ipaddress.txt')
    lines = f.readlines()
    f.close()
    for line in lines:
        subprocess.Popen(["ping", "-a", "-n", "l"]

Even if that code doesn't work it still should be the concept. You need to ping 192.168.0.1/255, and have the active ips displayed back to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM