This article provides a simple script to establish SSH connection and execute operational commands to retrieve output from multiple Juniper devices. The script can be modified to execute operational and configuration commands on multiple devices.
Netmiko is an open source SSH python library that simplifies the SSH management across wide range of network devices.
Prerequisites for successfully running the script:
Topology:
Notes:
'show'
#!/usr/local/bin/python3
From netmiko import ConnectHandler
import os directory = os.getcwd() conf = os.path.join(directory,input ("\n Name of Command File :-\n")) hostfile = os.path.join(directory,input ("\n Name of Host File :-\n")) username = 'jcluser' password = 'Juniper!1' platform = 'juniper_junos' port = '34005'
Open host file that contain list of host's IP to be connected.
with open(hostfile) as f: for line in f: host = line.strip() try: print ("\nConnecting ") net_connect = ConnectHandler(device_type=platform, ip=host, username=username, password=password, port = port) print ('\nConnected to {}, gathering info...\n'.format(host))
print ("\nConnecting ") net_connect = ConnectHandler(device_type=platform, ip=host, username=username, password=password, port = port) print ('\nConnected to {}, gathering info...\n'.format(host))
print(command) output = net_connect.send_command(command) print(output)
except:
print("Error running {} command".format(command))
show version | match Model show interface ge-0/0/0 terse show interface ge-0/0/2
66.129.234.200 66.129.234.201 66.129.234.202
Output:
Name of Command File: show_commands.txt Name of Host File: host_file.txt Connecting Connected to 66.129.234.200, gathering info... show version | match Model Model: vmx show interface ge-0/0/0 terse Interface Admin Link Proto Local Remote ge-0/0/0 up down show interface ge-0/0/2 Physical interface: ge-0/0/2, Enabled, Physical link is Down Interface index: 150, SNMP ifIndex: 528 Link-level type: Ethernet, MTU: 1514, MRU: 1522, LAN-PHY mode, Speed: 1000mbps, BPDU Error: None, Loop Detect PDU Error: None, Ethernet-Switching Error: None, MAC-REWRITE Error: None, Loopback: Disabled, Source filtering: Disabled, Flow control: Enabled, Auto-negotiation: Enabled, Remote fault: Online Pad to minimum frame size: Disabled Device flags : Present Running Down Interface flags: Hardware-Down SNMP-Traps Internal: 0x4000 Link flags : None CoS queues : 8 supported, 8 maximum usable queues Current address: 00:50:56:be:38:a7, Hardware address: 00:50:56:be:38:a7 Last flapped : 2021-12-30 13:06:26 UTC (19:03:10 ago) Input rate : 0 bps (0 pps) Output rate : 0 bps (0 pps) Active alarms : LINK Active defects : LINK PCS statistics Seconds Bit errors 0 Errored blocks 0 Ethernet FEC statistics Errors FEC Corrected Errors 0 FEC Uncorrected Errors 0 FEC Corrected Errors Rate 0 FEC Uncorrected Errors Rate 0 Interface transmit statistics: Disabled Connecting Connected to 66.129.234.201, gathering info... show version | match Model Model: vmx show interface ge-0/0/0 terse Interface Admin Link Proto Local Remote ge-0/0/0 up down show interface ge-0/0/2 Physical interface: ge-0/0/2, Enabled, Physical link is Down Interface index: 150, SNMP ifIndex: 528 Link-level type: Ethernet, MTU: 1514, MRU: 1522, LAN-PHY mode, Speed: 1000mbps, BPDU Error: None, Loop Detect PDU Error: None, Ethernet-Switching Error: None, MAC-REWRITE Error: None, Loopback: Disabled, Source filtering: Disabled, Flow control: Enabled, Auto-negotiation: Enabled, Remote fault: Online Pad to minimum frame size: Disabled Device flags : Present Running Down Interface flags: Hardware-Down SNMP-Traps Internal: 0x4000 Link flags : None CoS queues : 8 supported, 8 maximum usable queues Current address: 00:50:56:be:38:a7, Hardware address: 00:50:56:be:38:a7 Last flapped : 2021-12-30 13:06:26 UTC (19:03:19 ago) Input rate : 0 bps (0 pps) Output rate : 0 bps (0 pps) Active alarms : LINK Active defects : LINK PCS statistics Seconds Bit errors 0 Errored blocks 0 Ethernet FEC statistics Errors FEC Corrected Errors 0 FEC Uncorrected Errors 0 FEC Corrected Errors Rate 0 FEC Uncorrected Errors Rate 0 Interface transmit statistics: Disabled Connecting Connected to 66.129.234.202, gathering info… show version | match Model Model: vmx show interface ge-0/0/0 terse Interface Admin Link Proto Local Remote ge-0/0/0 up down show interface ge-0/0/2 Physical interface: ge-0/0/2, Enabled, Physical link is Down Interface index: 150, SNMP ifIndex: 528 Link-level type: Ethernet, MTU: 1514, MRU: 1522, LAN-PHY mode, Speed: 1000mbps, BPDU Error: None, Loop Detect PDU Error: None, Ethernet-Switching Error: None, MAC-REWRITE Error: None, Loopback: Disabled, Source filtering: Disabled, Flow control: Enabled, Auto-negotiation: Enabled, Remote fault: Online Pad to minimum frame size: Disabled Device flags : Present Running Down Interface flags: Hardware-Down SNMP-Traps Internal: 0x4000 Link flags : None CoS queues : 8 supported, 8 maximum usable queues Current address: 00:50:56:be:38:a7, Hardware address: 00:50:56:be:38:a7 Last flapped : 2021-12-30 13:06:26 UTC (19:03:19 ago) Input rate : 0 bps (0 pps) Output rate : 0 bps (0 pps) Active alarms : LINK Active defects : LINK PCS statistics Seconds Bit errors 0 Errored blocks 0 Ethernet FEC statistics Errors FEC Corrected Errors 0 FEC Uncorrected Errors 0 FEC Corrected Errors Rate 0 FEC Uncorrected Errors Rate 0 Interface transmit statistics: Disabled