Support Support Downloads Knowledge Base Case Manager My Juniper Community

Knowledge Base

Search our Knowledge Base sites to find answers to your questions.

Ask All Knowledge Base Sites All Knowledge Base Sites JunosE Defect (KA)Knowledge BaseSecurity AdvisoriesTechnical BulletinsTechnotes Sign in to display secure content and recently viewed articles

[Junos] Python script to scan a network subnet for Juniper hosts

0

0

Article ID: KB34732 KB Last Updated: 10 Jul 2019Version: 1.0
Summary:

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.

Symptoms:
  • There may be a large number of Juniper devices in the production environment which need to be inventoried or checked for a certain behavior or configuration consistency.
  • A method was required to discover the Juniper devices in a given subnet and list their host-names using a Python Script.
Solution:

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:

  1. Iterate through the hosts in a given subnet using the "ipaddress" Python library.

  2. Connect to Juniper devices using the PyEZ "Device" module". If connection is unsuccessful, print error and move on to next host.

  3. 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:  ".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
    
    

Related Links

Affected Products Browse the Knowledge Base for more articles related to these product categories. Select a category to begin.

Getting Up and Running with Junos

Getting Up and Running with Junos Security Alerts and Vulnerabilities Product Alerts and Software Release Notices Problem Report (PR) Search Tool EOL Notices and Bulletins JTAC User Guide Customer Care User Guide Pathfinder SRX High Availability Configurator SRX VPN Configurator Training Courses and Videos End User Licence Agreement Global Search