39 lines
1.7 KiB
Python
39 lines
1.7 KiB
Python
import re
|
|
|
|
# This script extracts ip, mac, and hostname from a dhcp server's log file to a csv file.
|
|
# This works only if the log files match the format in the example log file in this directory
|
|
|
|
def extract_info_from_logs(log_file_list, output_file_path):
|
|
# Regular expression to match IP address, MAC address, and hostname
|
|
regex = re.compile(
|
|
r'\b(?:DHCPACK|DHCPREQUEST|DHCPOFFER)\b.*?on\s+([\d.]+)\s+to\s+([\da-f:]+)(?:\s+\((.*?)\))?'
|
|
)
|
|
|
|
# A list of (ip, mac, host) data extracted from matched lines
|
|
extracted_data = []
|
|
|
|
for logfile in log_file_list:
|
|
with open(logfile, 'r') as logfile:
|
|
for line in logfile:
|
|
match = regex.search(line)
|
|
if match:
|
|
ip = match.group(1)
|
|
mac = match.group(2)
|
|
host = match.group(3) if match.group(3) else "Unknown"
|
|
data = ip + "," + mac + "," + host # Customizable as per requirements
|
|
if data not in extracted_data: # Ensure uniqueness
|
|
# Add other conditions here as per requirements
|
|
extracted_data.append(data) # Add extratcted data to the list
|
|
|
|
# Write the extracted data to the output file
|
|
with open(output_file_path, 'w') as output_file:
|
|
output_file.write("IP,MAC,HOST\n") # Header row
|
|
output_file.write('\n'.join(extracted_data)) # Data, as row+'\n'
|
|
|
|
print(f"Extraction complete. Data written to {output_file_path}.")
|
|
|
|
logfilelist = ['/var/log/syslog', '/var/log/syslog.1', '/var/log/dhcp.log'] # Specify the list of /path/log_file
|
|
outputfile = 'output.csv' # Specify the output file /path/name.csv
|
|
|
|
extract_info_from_logs(logfilelist, outputfile)
|