Skip to content

Commit e6b9f36

Browse files
committed
Extract method: parsing nmap output
1 parent 039fd16 commit e6b9f36

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

reconnoitre/ping_sweeper.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def ping_sweeper(target_hosts, output_directory, quiet):
99
output_file = output_directory + "/targets.txt"
1010

1111
print("[+] Writing discovered targets to: %s" % output_file)
12-
live_hosts = 0
1312
f = open(output_file, 'w')
1413

1514
print("[+] Performing ping sweep over %s" % target_hosts)
@@ -18,16 +17,22 @@ def ping_sweeper(target_hosts, output_directory, quiet):
1817
results = subprocess.check_output(SWEEP, shell=True)
1918
lines = str(results, "utf-8").split("\n")
2019

21-
for line in lines:
22-
line = line.strip()
23-
line = line.rstrip()
24-
if ("Nmap scan report for" in line):
25-
ip_address = line.split(" ")[4]
26-
if (live_hosts > 0):
27-
f.write('\n')
28-
f.write("%s" % (ip_address))
29-
print(" [>] Discovered host: %s" % (ip_address))
30-
live_hosts += 1
31-
print("[*] Found %s live hosts" % (live_hosts))
20+
live_hosts = parse_nmap_output_for_live_hosts(lines)
21+
f.write("\n".join(live_hosts))
22+
for ip_address in live_hosts:
23+
print(" [>] Discovered host: %s" % (ip_address))
24+
print("[*] Found %s live hosts" % (len(live_hosts)))
3225
print("[*] Created target list %s" % (output_file))
3326
f.close()
27+
28+
29+
def parse_nmap_output_for_live_hosts(lines):
30+
def get_ip_from_nmap_line(line):
31+
return line.split()[4]
32+
33+
live_hosts = [get_ip_from_nmap_line(line)
34+
for line in lines
35+
if "Nmap scan report for" in line]
36+
37+
return live_hosts
38+

0 commit comments

Comments
 (0)