-
Notifications
You must be signed in to change notification settings - Fork 1
/
MacLookup.py
executable file
·86 lines (68 loc) · 3.23 KB
/
MacLookup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# created by Alexander Deca - Deca Consulting 06/07/2023
# please note there is a requirements file -> pip install -r requirements.txt
# this script is a utility for network administrators to process and analyze the MAC addresses learned on a Cisco device
# and by consolidating them into a single CSV file. It uses specific libraries (ntc_templates and manuf) to facilitate
# parsing and MAC address vendor lookup.
import os
import csv
from ntc_templates.parse import parse_output
import manuf
import logging
logging.basicConfig(filename='error.log', level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def parse_cisco_show_output(output):
try:
# Parse the show command output using ntc-templates
result = parse_output(platform="cisco_ios", command="show mac address-table", data=output)
except Exception as e:
logger.error(f"Failed to parse output: {e}")
return []
parsed_results = [] # List to store parsed results
try:
mlookup = manuf.MacParser()
for entry in result:
mac_address = entry["destination_address"]
interface = entry["destination_port"][0]
mac_type = entry["type"][0]
vlan = entry["vlan"]
vendor = mlookup.get_manuf(mac_address)
if mac_type == 'S':
mac_type = "Static"
elif mac_type == 'D':
mac_type = "Dynamic"
if vendor is None:
vendor = "N/A"
parsed_results.append([mac_address, interface, mac_type, vlan, vendor])
except Exception as e:
logger.error(f"Failed to process parsed results: {e}")
return parsed_results
def review_directory(directory, output_file):
try:
# Create a CSV file for writing
with open(output_file, "w", newline="") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(["host", "mac_address", "interface", "mac_type", "vlan", "vendor"])
# Iterate over subdirectories in the given directory
for root, dirs, files in os.walk(directory):
for file in files:
# Check if the file is a show command output file
if file.startswith("show_mac") and file.endswith(".txt"):
file_path = os.path.join(root, file)
with open(file_path, "r") as f:
output = f.read()
parsed_results = parse_cisco_show_output(output)
# Write the extracted information to the CSV file
for entry in parsed_results:
writer.writerow([root.split("/")[1].split("_")[0]] + entry)
except Exception as e:
logger.error(f"Failed to review directory: {e}")
# Specify the directory path containing the subdirectories with show command output
directory_path = "output"
# Specify the output CSV file path
output_csv_file = "MacInfo.csv"
try:
# Review the directory and save the output in CSV format
review_directory(directory_path, output_csv_file)
except Exception as e:
logger.error(f"Failed to review directory and save output: {e}")