You are on page 1of 1

Write a python program to capture loopback0, IOS version, hostname from cisco

router, read input for ssh ip address from txt file, and give output in excel
Here is a Python program that will capture the loopback0, IOS version, and hostname
from a Cisco router using SSH, read the SSH IP address from a text file, and output
the results to an Excel file:

Copy code
import paramiko
import re
import xlsxwriter

# Read the IP address of the router from a text file


with open("router_ip.txt", "r") as f:
router_ip = f.read().strip()

# Connect to the router using SSH


client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(router_ip, username="admin", password="password")

# Execute the "show running-config" command and store the output


_, stdout, _ = client.exec_command("show running-config")
output = stdout.read().decode()

# Use regular expressions to extract the loopback0, IOS version, and hostname
loopback0_pattern = r"interface Loopback0\n +ip address (\d+\.\d+\.\d+\.\d+)"
ios_pattern = r"version (\d+\.\d+\.\d+)"
hostname_pattern = r"hostname (\S+)"
loopback0 = re.search(loopback0_pattern, output).group(1)
ios = re.search(ios_pattern, output).group(1)
hostname = re.search(hostname_pattern, output).group(1)

# Create an Excel workbook and add a worksheet


workbook = xlsxwriter.Workbook("router_info.xlsx")
worksheet = workbook.add_worksheet()

# Write the loopback0, IOS version, and hostname to the worksheet


worksheet.write("A1", "Loopback0")
worksheet.write("B1", "IOS version")
worksheet.write("C1", "Hostname")
worksheet.write("A2", loopback0)
worksheet.write("B2", ios)
worksheet.write("C2", hostname)

# Close the workbook


workbook.close()

# Close the SSH connection


client.close()
This program assumes that the router IP address is stored in a text file called
"router_ip.txt" and that the username and password for the router are "admin" and
"password", respectively.

To use this program, you will need to install the paramiko and xlsxwriter
libraries. You can do this by running the following command:

Copy code
pip install paramiko xlsxwriter
I hope this helps! Let me know if you have any questions.

You might also like