@@ -8,26 +8,39 @@ def ping_sweeper(target_hosts, output_directory, quiet):
8
8
check_directory (output_directory )
9
9
output_file = output_directory + "/targets.txt"
10
10
11
- print ("[+] Writing discovered targets to: %s" % output_file )
12
- live_hosts = 0
13
- f = open (output_file , 'w' )
14
-
15
11
print ("[+] Performing ping sweep over %s" % target_hosts )
16
12
17
- SWEEP = "nmap -n -sP %s" % (target_hosts )
18
- results = subprocess .check_output (SWEEP , shell = True ).decode ("utf-8" )
19
- lines = results .split ("\n " )
20
-
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 ))
13
+ lines = call_nmap_sweep (target_hosts )
14
+ live_hosts = parse_nmap_output_for_live_hosts (lines )
15
+ write_live_hosts_list_to_file (output_file , live_hosts )
16
+
17
+ for ip_address in live_hosts :
18
+ print (" [>] Discovered host: %s" % (ip_address ))
19
+
20
+ print ("[*] Found %s live hosts" % (len (live_hosts )))
32
21
print ("[*] Created target list %s" % (output_file ))
33
- f .close ()
22
+
23
+
24
+ def call_nmap_sweep (target_hosts ):
25
+ SWEEP = "nmap -n -sP %s" % (target_hosts )
26
+
27
+ results = subprocess .check_output (SWEEP , shell = True )
28
+ lines = str (results , "utf-8" ).split ("\n " )
29
+ return lines
30
+
31
+
32
+ def parse_nmap_output_for_live_hosts (lines ):
33
+ def get_ip_from_nmap_line (line ):
34
+ return line .split ()[4 ]
35
+
36
+ live_hosts = [get_ip_from_nmap_line (line )
37
+ for line in lines
38
+ if "Nmap scan report for" in line ]
39
+
40
+ return live_hosts
41
+
42
+
43
+ def write_live_hosts_list_to_file (output_file , live_hosts ):
44
+ print ("[+] Writing discovered targets to: %s" % output_file )
45
+ with open (output_file , 'w' ) as f :
46
+ f .write ("\n " .join (live_hosts ))
0 commit comments