Introduction:
In network administration, keeping track of hardware serial numbers is essential for inventory management, support, and warranty purposes. Manually retrieving these serial numbers from multiple switches can be time-consuming. Fortunately, with Python and the Paramiko library, you can automate the extraction of serial numbers from Cisco switches, saving time and reducing the risk of errors. This post walks you through a Python script designed to ping switches, connect via SSH, and extract only the switch's serial number (excluding SFPs or other connected devices), storing the results in an Excel file.
Prerequisites:
Before running the script, ensure you have the following:
- Python Installed: Make sure Python is installed on your machine. You can download it from python.org.
- Paramiko Library: Paramiko is a Python library used for making SSH connections. Install it using pip:
pip install paramiko
- Openpyxl Library: This library is used to create and manipulate Excel files. Install it using pip:
pip install openpyxl
- Switch Access: Ensure you have SSH access to the Cisco switches, with the correct username and password. The account should have at least read-only access.
- IP Address List: Prepare a text file containing the IP addresses of the switches you want to query. Save it as
Aug2024.txt
in the same directory as the script.
Script Explanation:
- Ping the Switches: The
check_switch
function sends 4 ping packets to each switch to verify it's reachable before attempting to connect via SSH. - Extract Serial Number: The
extract_switch_serial
function processes the output from theshow inventory
command, specifically extracting the serial number associated with the switch itself. - Save to Excel: The
save_to_excel
function creates an Excel workbook, writes the IP addresses and serial numbers to it, and saves the file with a timestamp in its name. - Main Function: This function orchestrates the entire process, reading IPs from
Aug2024.txt
, connecting to each switch, extracting the serial number, and saving the results to an Excel file.
Running the Script:
- Prepare the Environment: Ensure your Python environment has the necessary libraries installed and that your switches are reachable via SSH.
- Run the Script: Execute the script from your command line or IDE. You’ll be prompted for your SSH password. The script will then ping each switch, extract the serial number, and save the results in an Excel file.
- Check the Output: After the script completes, navigate to
D:\Python\Backup\SerialNumbers
(or your specified directory) to find the Excel file with the serial numbers.
Conclusion:
Automating the retrieval of serial numbers from Cisco switches not only saves time but also ensures accurate documentation. This Python script provides a robust solution for network administrators, especially when managing large-scale network environments. With minor modifications, this script can be adapted to suit various other network devices or different inventory tasks.