14
14
# Lock for writing to CSV file
15
15
csv_lock = threading .Lock ()
16
16
17
+ # Get the directory of the current script
18
+ script_dir = os .path .dirname (os .path .abspath (__file__ ))
17
19
18
20
def read_from_csv (filename ):
19
21
"""
@@ -25,7 +27,8 @@ def read_from_csv(filename):
25
27
Returns:
26
28
- data (list): A list of dictionaries representing rows from the CSV file.
27
29
"""
28
- with open (filename , 'r' ) as file :
30
+ filepath = os .path .join (script_dir , filename )
31
+ with open (filepath , 'r' ) as file :
29
32
reader = csv .DictReader (file )
30
33
data = [row for row in reader ]
31
34
return data
@@ -42,8 +45,9 @@ def remove_scanned_prefixes(data, scanned_prefixes):
42
45
updated_data = [row for row in data if row ['Prefix' ] not in scanned_prefixes ]
43
46
44
47
# Rewrite the updated data to the CSV file
45
- with open ('ipam_prefixes.csv' , 'w' , newline = '' ) as file :
46
- fieldnames = ['Prefix' , 'VRF' , 'Status' , 'Tags' , 'Tenant' ] # Added 'VRF' to fieldnames
48
+ filepath = os .path .join (script_dir , 'ipam_prefixes.csv' )
49
+ with open (filepath , 'w' , newline = '' ) as file :
50
+ fieldnames = ['Prefix' , 'VRF' , 'Status' , 'Tags' , 'Tenant' ]
47
51
writer = csv .DictWriter (file , fieldnames = fieldnames )
48
52
writer .writeheader ()
49
53
writer .writerows (updated_data )
@@ -110,12 +114,15 @@ def run_nmap_on_prefixes(data, output_folder):
110
114
results = []
111
115
scanned_prefixes = []
112
116
117
+ # Create the full path for the output folder
118
+ output_folder_path = os .path .join (script_dir , output_folder )
119
+
113
120
# Filter rows to scan only those with status 'active' and without the tag 'Disable Automatic Scanning'
114
121
rows_to_scan = [row for row in data if row ['Status' ] == 'active' and 'Disable Automatic Scanning' not in row ['Tags' ]]
115
122
116
123
script_start_time = datetime .now () # Get the script start time
117
124
118
- with ThreadPoolExecutor (max_workers = 5 ) as executor : # Adjust the max_workers parameter based on your system's capabilities
125
+ with ThreadPoolExecutor (max_workers = 5 ) as executor :
119
126
# Use executor.map to asynchronously run the scans and get results
120
127
futures = {executor .submit (run_nmap_on_prefix , row ['Prefix' ], row ['Tenant' ], row ['VRF' ]): row for row in rows_to_scan }
121
128
@@ -125,8 +132,7 @@ def run_nmap_on_prefixes(data, output_folder):
125
132
with csv_lock :
126
133
results .extend (prefix_results )
127
134
scanned_prefixes .append (futures [future ]['Prefix' ])
128
- write_results_to_csv (prefix_results , output_folder , script_start_time ) # Pass script start time
129
-
135
+ write_results_to_csv (prefix_results , output_folder_path , script_start_time )
130
136
131
137
remove_scanned_prefixes (data , scanned_prefixes )
132
138
return results
@@ -151,8 +157,8 @@ def write_results_to_csv(results, output_folder, script_start_time):
151
157
# Check if the file is empty
152
158
is_empty = not os .path .exists (output_filename ) or os .stat (output_filename ).st_size == 0
153
159
154
- with open (output_filename , 'a' , newline = '' ) as file : # Use 'a' (append) mode to add results to the file
155
- fieldnames = ['address' , 'dns_name' , 'status' , 'tags' , 'tenant' , 'VRF' , 'scantime' ] # Added 'VRF' to fieldnames
160
+ with open (output_filename , 'a' , newline = '' ) as file :
161
+ fieldnames = ['address' , 'dns_name' , 'status' , 'tags' , 'tenant' , 'VRF' , 'scantime' ]
156
162
writer = csv .DictWriter (file , fieldnames = fieldnames )
157
163
158
164
# Add headers if the file is empty
@@ -165,4 +171,4 @@ def write_results_to_csv(results, output_folder, script_start_time):
165
171
if __name__ == "__main__" :
166
172
data = read_from_csv ('ipam_prefixes.csv' )
167
173
output_folder = 'results'
168
- run_nmap_on_prefixes (data , output_folder )
174
+ run_nmap_on_prefixes (data , output_folder )
0 commit comments