-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path140-fmad-exec-nmap-arp-scan.py
99 lines (73 loc) · 2.64 KB
/
140-fmad-exec-nmap-arp-scan.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
97
98
99
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
The final goal is finding free addresses in a network range
Not finished but you may see how it looks like
"""
import sys
import subprocess
"""
execthis()
a function that runs an arbitrary command with its args
and returns the contents of the standard output in case of success
or the error text in case of disaster
"""
def execthis(cmdargs, pattern):
""" create a subprocess "p" and pipe stdout and stderr
"""
p = subprocess.run(cmdargs, stdin=None, stdout=subprocess.PIPE,
input=None, stderr=subprocess.PIPE,
shell=False, timeout=None, check=False, universal_newlines=True)
""" get the result of the execution (much like "$?" in bash) and add it to the "output" list
"""
rc = p.returncode
rv = []
""" in case of success, the subprocess returns zero, so "not rc" is true
and flow goes through the "if"; in case of error will run the "else"
"""
if not rc:
for item, line in enumerate(p.stdout.split("\n")):
if pattern in line:
words = line.split(" ")
rv.append(words[len(words) - 1])
else:
for item, line in enumerate(p.stderr.split("\n")):
rv.append(line)
return rc, rv
"""
HERE it comes the very very true stuff
"""
if __name__ == "__main__":
myself = sys.argv[0]
print("Started {}...".format(myself))
rc = 0
for index, item in enumerate(sys.argv[1:]):
""" we plan to execute nmap to build the range of addresses.
check it's installed in your system
"""
cmdargs = ["nmap", "-sL", "-n", str(item)]
rc, rv = execthis(cmdargs, "Nmap scan report for")
""" now check nmap's return code
"""
if rc:
print("ARGH! Rc is {:04d}".format(rc))
""" print the address list in case of success, or the stderr contents if something goes wrong
"""
for outline in enumerate(rv):
""" arp-scan tests each address for presence or absence of a system
again, check it's installed
"""
print(outline)
ipaddress = outline[1]
cmdargs2 = ["sudo", "arp-scan", ipaddress]
rc2, rv2 = execthis(cmdargs2, ipaddress)
if not rc2:
if len(arpproc) > 1:
print("USED... ", end="")
else:
print("FREE... {}".format(outline))
else:
print("ARGH! Rc is {:04d}".format(rc2))
print("Ended {} with code {}".format(myself, rc2))
""" THE END
"""