-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingsweep.py
More file actions
57 lines (49 loc) · 1.72 KB
/
pingsweep.py
File metadata and controls
57 lines (49 loc) · 1.72 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python3
import platform
import subprocess
import sys
PLATFORM_PARAM = '-n' if platform.system() == 'Windows' else '-c' #set platform parameter
#check arguments
if len(sys.argv) <= 1:
print("You need to specify a network range. E.g. 10.0.2")
aux = input("Range: ")
try:
if len(aux.split('.')) == 3:
host = aux + '.'
else:
while len(aux.split('.')) != 3:
aux = input("\n\nWrong format.\nTry again:")
host = aux + '.'
except:
print("\n\nSomething went wrong.")
sys.exit(1)
else:
host = sys.argv[1] + '.'
cmd2 = ['grep', '64 bytes']
cmd3 = ['cut', '-d ', '-f', '4']
cmd4 = ['tr', '-d', ':']
cmd5 = ['tr', '-d', '"\n"']
#do the sweep
if platform.system() == 'Windows':
#for Windows
for ip in range(1,254):
#set command and execute
cmd1 = ['ping', PLATFORM_PARAM, '1', host + str(ip)]
subprocess.run(cmd1)
else:
#other OS
for ip in range(1,254):
#set command
cmd1 = ['ping', PLATFORM_PARAM, '1', host + str(ip)]
p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p3 = subprocess.Popen(cmd3, stdin=p2.stdout, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p4 = subprocess.Popen(cmd4, stdin=p3.stdout, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p5 = subprocess.Popen(cmd5, stdin=p4.stdout, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
#execute
stdout, stderr = p5.communicate()
if stdout and stdout != None:
print(stdout.decode('utf-8'))
else:
if stderr:
print(stderr)