T
T
Tremo2016-08-01 09:32:31
Python
Tremo, 2016-08-01 09:32:31

How to solve the problem of compilation error in Python 3?

I want to make a small tool that will include several different scanning methods.
One of them had a problem. This part of the script pings all hosts on the specified network.
I found a suitable script and I can not run it gives the following error:

info = subprocess.STARTUPINFO()
AttributeError: module 'subprocess' has no attribute 'STARTUPINFO'


script:
import modules
import subprocess
import ipaddress

# Prompt the user to input a network address
net_addr= input("Enter a network address in CIDR format(ex.192.168.1.0/24): ")
# Create the network
ip_net = ipaddress.ip_network(net_addr)

# Get all hosts on that network
all_hosts = list(ip_net.hosts())

# Configure subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

# For each IP address in the subnet run the ping command with subprocess.popen interface
for i in range(len(all_hosts)):
    output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

    if "Destination host unreachable" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    elif "Request timed out" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    else:
        print(str(all_hosts[i]), "is Online")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-08-01
@tremo0880

The STARTUPINFO class is only available on Windows.
Looks like the only solution is not to set these flags on posix systems.

info = None
if os.name == 'nt':
     info = subprocess.STARTUPINFO()
     info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
     info.wShowWindow = subprocess.SW_HIDE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question