-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathping-sweeper.py
64 lines (54 loc) · 2.11 KB
/
ping-sweeper.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
""" PING SWEEPER
A lightweight script to probe with ICMP packets for asset discovery on a network.
"""
from platform import system as system_name # Returns the system/OS name
import subprocess
import ipcalc
# ICMP probe
def ping(host):
#Changes parameter based on OS
param = '-n' if system_name().lower()=='windows' else '-c'
output = subprocess.Popen(["ping", host, param, '1'], stdout=subprocess.PIPE).communicate()[0]
# and then check the response...
if b'unreachable' in output:
print(f"Connection to {host} Unreachable!")
elif b'try' in output:
print(f'Connection to {host} Failed!')
elif b'Request timed out' in output:
print(f"Connection to {host} Timed Out!")
else:
print(f"Connection to {host} Passed!")
def ip_calc(IP, Format):
Format = Format
if Format == '1':
start_ip, end_ip = IP.split('-')
start_range = int(start_ip.split('.')[-1])
end_range = int(end_ip.split('.')[-1])
template_ip = start_ip.split('.')[0] + '.' + start_ip.split('.')[1] + '.' + start_ip.split('.')[2] + '.'
for x in range(start_range, end_range + 1):
current_ip = template_ip + str(x)
ping(current_ip)
elif Format == '2':
for ips in ipcalc.Network(IP):
ping(str(ips))
else:
print('wong format')
# running out in main
if __name__ == '__main__':
try:
Format = input("""
1 -> 192.168.1.1-182.168.1.255
2 -> 192.168.1.1/24 """)
domain_range = input("Specify the addresses in choosen format ")
ip_calc(domain_range, Format)
# start_ip, end_ip = domain_range.split('-')
# start_range = int(start_ip.split('.')[-1])
# end_range = int(end_ip.split('.')[-1])
# template_ip = start_ip.split('.')[0] + '.' + start_ip.split('.')[1] + '.' + start_ip.split('.')[2] + '.'
# for x in range(start_range, end_range + 1):
# current_ip = template_ip + str(x)
# ping(current_ip)
except KeyboardInterrupt:
print('user hard exited')
except Exception as e:
print(e)