Description

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.

Solution

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:

  • Proper authorization and authentication parameters are defined for the workstation that will be used to log in to Junos devices.
  • Python and Netmiko are installed on the workstation.

Topology:

Notes:

  • Username and password are stored in the script for lab purposes. The more secure method to authenticate device connection is using a public/private key pair or use a getpass().
  • For reference, the 'show' commands that are to be executed are stored in show_commands.txt file and IP address of the Junos device is in host_file.txt.
  • The Python script, command file, and host file are located in the same directory. The script can be modified to take the absolute path of command file and host file.
  • In use are Netmiko version 3.4.0 and python version 3.10.1.

Solution:
-------------------------------------------------
Scripts % cat netmiko_script.py 
-------------------------------------------------
 
#!/usr/local/bin/python3
 
  1. 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'
  2. 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))

  3. Open Config file that needs to be added on device.
    with open(conf) as f:
       for line in f:
          command=line.strip()
          try:
        print(command)
        output = net_connect.send_command(command)
        print(output)

          except:

              print("Error running {} command".format(command))

          except:

              print ('Error Connecting {}'.format(host))
-------------------------------------------------
Scripts % cat show_commands.txt 
-------------------------------------------------
show version | match Model
show interface ge-0/0/0 terse
show interface ge-0/0/2
-------------------------------------------------
Scripts % cat host_file.txt 
-------------------------------------------------
66.129.234.200
66.129.234.201
66.129.234.202
 

Output:

-------------------------------------------------
Scripts % python3 netmiko_script.py
-------------------------------------------------
 
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