-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathping_network.py
96 lines (81 loc) · 3.84 KB
/
ping_network.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
import subprocess
import ipaddress
import sys
import os
from common import bcolors, pressKey
def pingNetwork(network=None, stdout=True):
if network == None:
# Prompt the user to input a network address
if (sys.version_info > (3, 0)):
net_addr = input(
"\nEnter a network address in CIDR format (ex.192.168.1.0/24)\n o enter desired ip: ")
else:
net_addr = raw_input("\nIntroduce la dirección de red en formato CIDR (ex.192.168.1.0/24)\n o introduce una ip: ")
net_addr = net_addr.decode('utf-8')
# Create the network
try:
ip_net = ipaddress.ip_network(net_addr, strict=False)
# print(ip_net)
except ValueError:
print(
"\nEl valor " + bcolors.FAIL + net_addr + bcolors.ENDC + " no parece ser un formato de red IPv4 o IPv6\n")
pressKey()
return None
else:
net_addr = network
if (sys.version_info < (3, 0)):
net_addr = net_addr.decode('utf-8')
ip_net = ipaddress.ip_network(net_addr, strict=False)
#print(ip_net)
# Get all hosts on that network
all_hosts = list(ip_net.hosts())
if len(all_hosts) == 0:
all_hosts.append(net_addr)
#print(all_hosts)
# Configure subprocess to hide the console window
if os.name == 'nt':
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 host in all_hosts:
if os.name == 'nt':
output = subprocess.Popen(['ping', '-n', '1', '-w', '5000', str(host)], stdout=subprocess.PIPE,
startupinfo=info).communicate()[0]
else:
output = subprocess.Popen(['ping', '-c', '1', '-W', '5', str(host)], stdout=subprocess.PIPE).communicate()[0]
#print(output)
if "destination host unreachable" in output.decode('utf-8', errors='ignore').lower() or "host de destino inaccesible" in output.decode('utf-8', errors='ignore').lower():
if network == None:
if stdout:
print("\n" + str(host) + " : " + bcolors.FAIL + "Offline" + bcolors.ENDC)
else:
if stdout:
print("[ " + str(network) + " ] : " + bcolors.FAIL + "Offline" + bcolors.ENDC)
return False
elif "request timed out" in output.decode('utf-8', errors='ignore').lower() or "tiempo de espera agotado" in output.decode('utf-8', errors='ignore').lower():
if network == None:
if stdout:
print("\n" + str(host) + " : " + bcolors.FAIL + "Offline" + bcolors.ENDC)
else:
if stdout:
print("[ " + str(network) + " ] : " + bcolors.FAIL + "Offline" + bcolors.ENDC)
return False
elif "100% loss" in output.decode('utf-8', errors='ignore').lower() or "100% perdidos" in output.decode('utf-8', errors='ignore').lower() or "100% packet loss" in output.decode('utf-8', errors='ignore').lower():
if network == None:
if stdout:
print("\n" + str(host) + " : " + bcolors.FAIL + "Offline" + bcolors.ENDC)
else:
if stdout:
print("[ " + str(network) + " ] : " + bcolors.FAIL + "Offline" + bcolors.ENDC)
return False
else:
if network == None:
if stdout:
print("\n" + str(host) + " : " + bcolors.OKGREEN + "Online" + bcolors.ENDC)
else:
if stdout:
print("[ " + str(network) + " ] : " + bcolors.OKGREEN + "Online" + bcolors.ENDC)
return True