-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingwindows.py
More file actions
38 lines (30 loc) · 1.32 KB
/
pingwindows.py
File metadata and controls
38 lines (30 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# import socket
# socket_obj=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# socket.setdefaulttimeout(1)
# result=socket_obj.connect_ex((addr,port))
# socket_obj.close()
# Import modules
import subprocess
import ipaddress
import socket
# 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]
output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE).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")