import os import re from dotenv import load_dotenv from netmiko.cisco.cisco_wlc_ssh import CiscoWlcSSH import pandas as pd load_dotenv() ip = input("Please input the WLC IP: ") user = os.environ.get("USER") password = os.environ.get("PASS") wlc = CiscoWlcSSH( ip=ip, username=user, password=password ) wlc.send_command("config paging disable") ap_summary = wlc.send_command("show ap summary", read_timeout=30) pattern = re.compile(r""" (?P\S+)\s+ # AP Name (non-space characters followed by spaces) (?P\d+)\s+ # Slots (digits followed by spaces) (?P[\w\-]+)\s+ # AP Model (alphanumeric or hyphen) (?P[0-9a-fA-F:]+)\s+ # Ethernet MAC address (hexadecimal, colon-separated) (?P[\w\s\-]+)\s+ # Location (alphanumeric, space, or hyphen) (?P\w+)\s+ # Country (two uppercase letters) (?P\d+\.\d+\.\d+\.\d+)\s+ # IP Address (IPv4 format) (?P\d+)\s+ # Clients (digits) (?P\[.*\]) # DSE Location (list format) """, re.VERBOSE) # List to store parsed dictionaries parsed_data = [] # Process each row in the text (excluding the header and separator) for line in ap_summary.strip().splitlines()[2:]: match = pattern.match(line) if match: parsed_data.append(match.groupdict()) # Create a DataFrame from the parsed data df = pd.DataFrame(parsed_data) # Define the Excel file name file_name = 'ap_data.xlsx' with pd.ExcelWriter(file_name, engine='openpyxl') as writer: # Write the dataframe to the first sheet without the header row df.to_excel(writer, index=False, header=False, sheet_name='AP Data') # Access the workbook and the sheet workbook = writer.book worksheet = workbook['AP Data'] from openpyxl.utils import get_column_letter # Set the column headers to letters (A, B, C, D, etc.) for idx in range(len(df.columns)): # Convert the column index (starting from 0) to Excel letters (A, B, C, ...) worksheet[get_column_letter(idx + 1) + '1'].value = get_column_letter(idx + 1) # Set the column width for better readability column_widths = {} for col in df.columns: max_length = max(df[col].astype(str).map(len).max(), len(col)) + 2 # Add a bit of padding column_widths[col] = max_length for idx, width in enumerate(column_widths.values()): worksheet.column_dimensions[get_column_letter(idx + 1)].width = width # Add a bold header row (A, B, C, D, etc.) for cell in worksheet[1]: cell.font = cell.font.copy(bold=True) # Let the user know the file has been saved print(f"Excel file '{file_name}' has been created successfully.")