router interface list reading through ssh and python script to read traffic over a particular interface

312 Views Asked by At

Hello everyone، I am new to python and netmiko. I want to read multiple interfaces on a Cisco router to monitor live traffic's by using python script through a netmiko ssh connection.

There should be three steps 1, ssh 2,read interface list 3, read interface traffic

1

There are 1 best solutions below

1
pyjedy On

Example of using the Netmiko library to establish an SSH connection to a Cisco IOS device, retrieve interface information using the 'show interfaces' command, and parse the output using NTC templates.

from netmiko import ConnectHandler
from ntc_templates.parse import parse_output

# Define the device details
device = {
    "device_type": "cisco_ios",
    "ip": "10.0.0.1",
    "username": "username",
    "password": "password.",
}

# Establish SSH connection
ssh = ConnectHandler(**device)

# Send command to retrieve interface list
output = ssh.send_command("show interfaces")
print(output)
# GigabitEthernet0/0/0 is up, line protocol is up
#   ...
#   30 second input rate 4000 bits/sec, 2 packets/sec
#   30 second output rate 94000 bits/sec, 52 packets/sec
#
# GigabitEthernet0/0/1 is up, line protocol is up
#   ...
#   30 second input rate 3590000 bits/sec, 449 packets/sec
#   30 second output rate 123000 bits/sec, 160 packets/sec
# ...

# Input rate, and output rate are printed for each interface.
parsed_output = parse_output("cisco_ios", "show interfaces", output)
for data in parsed_output:
    print("{interface} {input_rate} {output_rate}".format(**data))
# GigabitEthernet0/0/0 4000 94000
# GigabitEthernet0/0/1 3590000 123000
# ...