2016-12-07 36 views
1

我正在嘗試編寫一個小型網絡掃描器來玩。子進程模塊沒有任何屬性STARTUPINFO

代碼:

# Configure subprocess to hide the console window 

info = subprocess.STARTUPINFO() 

info.dwFlags |= subprocess.STARTF_USESHOWWINDOW 

info.wShowWindow = subprocess.SW_HIDE 

這是導致問題的塊。在運行時,我得到以下錯誤:

Enter a network address in CIDR format(ex.192.168.1.0/24): 192.168.1.1 
Traceback (most recent call last): 
    File "scannerMock.py", line 17, in <module> 
    info = subprocess.STARTUPINFO() 
AttributeError: module 'subprocess' has no attribute 'STARTUPINFO' 

我在網上看了看四周,重新安裝了子模塊無濟於事,任何想法,爲什麼它這樣做呢?

全碼:

# Import modules 
import subprocess 
import ipaddress 
import os 
import sys 
# 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 
#startupinfo = None 
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', '-c', '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") 
+0

你是否得到這個工作?我有同樣的問題。 –

回答

0

你可能已經重挫subprocess莫名其妙。做print subprocess.__ file__,我敢打賭它不是核心。

+0

它來自核心。我得到 - /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py 並得到相同的錯誤 - AttributeError:模塊'subprocess'沒有屬性'STARTUPINFO' –

+0

你在哪裏運行檢查?你在上面顯示的文件裏面? – joeb

+0

是的,就在進口報表之後。 –

相關問題