@@ -8,39 +8,29 @@ 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 ("[+] Performing ping sweep over %s" % target_hosts )
12
-
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 )))
21
- print ("[*] Created target list %s" % (output_file ))
22
-
11
+ print ("[+] Writing discovered targets to: %s" % output_file )
12
+ live_hosts = 0
13
+ f = open (output_file , 'w' )
23
14
24
- def call_nmap_sweep (target_hosts ):
25
- SWEEP = "nmap -n -sP %s" % (target_hosts )
15
+ print ("[+] Performing ping sweep over %s" % target_hosts )
26
16
17
+ if (os .path .isfile (target_hosts )):
18
+ SWEEP = "nmap -n -sP -iL %s" % (target_hosts )
19
+ else :
20
+ SWEEP = "nmap -n -sP %s" % (target_hosts )
27
21
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 ))
22
+ lines = results .split ("\n " )
23
+
24
+ for line in lines :
25
+ line = line .strip ()
26
+ line = line .rstrip ()
27
+ if ("Nmap scan report for" in line ):
28
+ ip_address = line .split (" " )[4 ]
29
+ if (live_hosts > 0 ):
30
+ f .write ('\n ' )
31
+ f .write ("%s" % (ip_address ))
32
+ print (" [>] Discovered host: %s" % (ip_address ))
33
+ live_hosts += 1
34
+ print ("[*] Found %s live hosts" % (live_hosts ))
35
+ print ("[*] Created target list %s" % (output_file ))
36
+ f .close ()
0 commit comments