Skip to content

Commit

Permalink
Added vrf
Browse files Browse the repository at this point in the history
  • Loading branch information
LoH-lu committed May 6, 2024
1 parent 0c18d09 commit 6f66948
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
6 changes: 5 additions & 1 deletion netbox_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ def process_row(row, pbar):
existing_address.tags = tags_list
if row['tenant'] != 'N/A': # Check if tenant is not 'N/A'
existing_address.tenant = {'name': row['tenant']}
if row['VRF'] != 'N/A': # Check if VRF is not 'N/A'
existing_address.vrf = {'name': row['VRF']}
existing_address.save()
else:
try:
# Create a new address if it doesn't exist
tenant_data = {'name': row['tenant']} if row['tenant'] != 'N/A' else None
vrf_data = {'name': row['VRF']} if row['VRF'] != 'N/A' else None
netbox.ipam.ip_addresses.create(
address=row['address'],
status=row['status'],
description=row['description'],
dns_name=row['dns_name'],
tags=tags_list,
tenant=tenant_data
tenant=tenant_data,
vrf=vrf_data
)
except pynetbox.core.query.RequestError as e:
# Handle duplicate address error
Expand Down
5 changes: 3 additions & 2 deletions netbox_retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ def write_to_csv(data, filename):
file_path = os.path.join(script_dir, filename) # Construct the full path to the output file
with open(file_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Prefix', 'Status', 'Tags', 'Tenant']) # Writing headers
writer.writerow(['Prefix', 'VRF', 'Status', 'Tags', 'Tenant']) # Writing headers
for prefix in data:
tag_names = [tag.name for tag in prefix.tags]
tenant_name = prefix.tenant.name if prefix.tenant else 'N/A'
status_value = prefix.status.value if prefix.status else 'N/A' # Extract the value of the status field
writer.writerow([prefix.prefix, status_value, ', '.join(tag_names), tenant_name])
vrf_name = prefix.vrf.name if prefix.vrf else 'N/A' # Extract the name of the VRF
writer.writerow([prefix.prefix, vrf_name, status_value, ', '.join(tag_names), tenant_name])

# Read URL and token from var.ini
config = configparser.ConfigParser()
Expand Down
2 changes: 1 addition & 1 deletion nmap_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def write_csv(data, file_path):
- file_path (str): The path to the output CSV file.
"""
with open(file_path, 'w', newline='') as file:
fieldnames = ['address', 'dns_name', 'status', 'description', 'tags', 'tenant']
fieldnames = ['address', 'dns_name', 'status', 'description', 'tags', 'tenant', 'VRF'] # Added 'VRF' to fieldnames
writer = csv.DictWriter(file, fieldnames=fieldnames)

# Write header
Expand Down
14 changes: 9 additions & 5 deletions nmap_scan_multi_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ def remove_scanned_prefixes(data, scanned_prefixes):

# Rewrite the updated data to the CSV file
with open('ipam_prefixes.csv', 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=['Prefix', 'Status', 'Tags', 'Tenant'])
fieldnames = ['Prefix', 'VRF', 'Status', 'Tags', 'Tenant'] # Added 'VRF' to fieldnames
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(updated_data)

def run_nmap_on_prefix(prefix, tenant):
def run_nmap_on_prefix(prefix, tenant, vrf):
"""
Run nmap scan on a given prefix.
Args:
- prefix (str): The prefix to be scanned.
- tenant (str): The tenant associated with the prefix.
- vrf (str): The VRF associated with the prefix.
Returns:
- results (list): A list of dictionaries containing scan results.
Expand Down Expand Up @@ -81,7 +83,8 @@ def run_nmap_on_prefix(prefix, tenant):
'status': 'active',
'description': 'Scanned IP address',
'tags': 'autoscan',
'tenant': tenant
'tenant': tenant,
'VRF': vrf # Add VRF to the results
})
print(f"Finished scan on prefix: {prefix}")
return results, True
Expand All @@ -102,7 +105,7 @@ def run_nmap_on_prefixes(data, output_folder):

with ThreadPoolExecutor(max_workers=5) as executor: # Adjust the max_workers parameter based on your system's capabilities
# Use executor.map to asynchronously run the scans and get results
futures = {executor.submit(run_nmap_on_prefix, row['Prefix'], row['Tenant']): row for row in rows_to_scan}
futures = {executor.submit(run_nmap_on_prefix, row['Prefix'], row['Tenant'], row['VRF']): row for row in rows_to_scan}

for future in concurrent.futures.as_completed(futures):
prefix_results, success = future.result()
Expand Down Expand Up @@ -135,7 +138,8 @@ def write_results_to_csv(results, output_folder):
is_empty = not os.path.exists(output_filename) or os.stat(output_filename).st_size == 0

with open(output_filename, 'a', newline='') as file: # Use 'a' (append) mode to add results to the file
writer = csv.DictWriter(file, fieldnames=['address', 'dns_name', 'status', 'description', 'tags', 'tenant'])
fieldnames = ['address', 'dns_name', 'status', 'description', 'tags', 'tenant', 'VRF'] # Added 'VRF' to fieldnames
writer = csv.DictWriter(file, fieldnames=fieldnames)

# Add headers if the file is empty
if is_empty:
Expand Down

0 comments on commit 6f66948

Please sign in to comment.