This article provides a simple method to scan a given network subnet to list host names and IP addresses all Junos devices present in it. The basic code can be modified easily to perform configuration changes or to collect any required information from those devices using NETCONF over SSH.
One simple way of scanning through a subnet to list the Junos device host names and product models is by implementing the following steps in a Python Script:
Iterate through the hosts in a given subnet using the "ipaddress" Python library.
Connect to Juniper devices using the PyEZ "Device" module". If connection is unsuccessful, print error and move on to next host.
Print the host name. Other details like HW information can also be included if you want.
Sample code:
nw=ipaddress.ip_network('10.85.173.128/25') for addr in nw: dev=Device(host=str(addr),user=####,password=####) try: dev.open() except ConnectError as err: print ("Cannot connect to device: {0.EN_US}".format(err)) continue sw=dev.rpc.get_software_information() hn=sw.findtext('host-name') mod=sw.findtext('product-model') print("Hostname of device with IP=", str(addr), "is", str(hn), "Product Model =", str(mod)) dev.close()
Sample Script Run:
Cannot connect to device: ConnectTimeoutError(10.85.173.128) Cannot connect to device: ConnectRefusedError(10.85.173.129) Cannot connect to device: ConnectRefusedError(10.85.173.130) Cannot connect to device: ConnectRefusedError(10.85.173.131) Device(10.85.173.132) Hostname of device with IP= 10.85.173.132 is bacardi Product Model = ex9214 Cannot connect to device: ConnectTimeoutError(10.85.173.133) Device(10.85.173.134) Hostname of device with IP= 10.85.173.134 is bacardi Product Model = ex9214 Device(10.85.173.135) Hostname of device with IP= 10.85.173.135 is m02-17 Product Model = qfx10002-72q Device(10.85.173.136) Hostname of device with IP= 10.85.173.136 is m02-19 Product Model = qfx10002-72q Device(10.85.173.137) Hostname of device with IP= 10.85.173.137 is m02-21 Product Model = qfx10002-72q
Python Libraries required:
import ipaddress from ipaddress import ip_network import jnpr.junos from jnpr.junos.exception import LockError from jnpr.junos.exception import ConnectError from jnpr.junos import Device import lxml from lxml import etree import warnings