Automation

Expand all | Collapse all

Python script of LLDP (Not using Pyezy)

Jump to Best Answer
  • 1.  Python script of LLDP (Not using Pyezy)

    Posted 02-04-2019 16:23

    Hi Junos Experts,

    I have seen most of the automation script example is using cisco cdp and less example of using lldp or junos environment. Some of them using pyezy and unfortunately my current juniper environment does not have netconf. 

    Is there any good lldp script that use python? Some example would be good for starting.

    Thank you for your support and further advise.



  • 2.  RE: Python script of LLDP (Not using Pyezy)
    Best Answer

     
    Posted 02-05-2019 04:35

    Hi jar,

     

    You can mention port 22 to use SSH to connect to the device, but need the juniper modules hence PyEZ is needed on your Automation server/machine.  Outputs you get with this will include the tags from "show lldp neighbors | display xml" that you could play around with.  I've shared a couple of examples to help here.  Please check this out:

     

    Example 1: (basic LLDP info script and output)

    ---------------

    ####START - Script to get LLDP info from a Juniper device using SSH and Python

    from jnpr.junos import Device
    from lxml import etree


    dev = Device(host="1.1.1.1", user="labroot", password="lab123", port=22, normalize=True)
    dev.open()
    resp = dev.rpc.get_lldp_neighbors_information()

    #NOTE: Modifying above line to "resp = dev.rpc.get_lldp_neighbors_information(normalize=False)" will give XML output
    print (etree.tostring(resp,encoding='unicode'))
    dev.close()

     

    ####END

     

    Device config and CLI output:

    -------------------------------------

    labroot@R2> show configuration system services
    ftp;
    ssh;

     

    {master:0}
    labroot@R2> show lldp neighbors
    Local Interface Parent Interface Chassis Id Port info System Name
    xe-0/0/7 - 0c:86:10:7f:8a:00 xe-0/0/7 R4
    xe-0/0/3 - 0c:86:10:7f:8a:00 xe-0/0/3 R4
    xe-0/0/8 - 0c:86:10:7f:8a:00 xe-0/0/8 R4
    xe-0/0/9 - 0c:86:10:7f:8a:00 xe-0/0/9 R4
    em0 - c0:42:d0:24:e0:40 ge-0/0/24.0 QNC-JTAC-LSW-C15-52
    xe-0/0/2 - f4:b5:2f:4a:33:00 xe-0/0/2 R3

     

    Sample Script Output: (prints all output in one line)

    ------------------------------
    C:\Python27>python lldp_info_basic.py
    <lldp-neighbors-information style="brief"><lldp-neighbor-information><lldp-local-port-id>xe-0/0/7</lldp-local-port-id><lldp-local-parent-interface-name>-</lldp-local-parent-interface-name><lldp-remote-chassis-id-subtype>Mac address</lldp-remote-chassis-id-subtype><lldp-remote-chassis-id>0c:86:10:7f:8a:00</lldp-remote-chassis-id><lldp-remote-port-description>xe-0/0/7</lldp-remote-port-description><lldp-remote-system-name>R4</lldp-remote-system-name></lldp-neighbor-information><lldp-neighbor-information><lldp-local-port-id>xe-0/0/3</lldp-local-port-id><lldp-local-parent-interface-name>-</lldp-local-parent-interface-name><lldp-remote-chassis-id-subtype>Mac address</lldp-remote-chassis-id-subtype><lldp-remote-chassis-id>0c:86:10:7f:8a:00</lldp-remote-chassis-id><lldp-remote-port-description>xe-0/0/3</lldp-remote-port-description><lldp-remote-system-name>R4</lldp-remote-system-name></lldp-neighbor-information><lldp-neighbor-information><lldp-local-port-id>xe-0/0/8</lldp-local-port-id><lldp-local-parent-interface-name>-</lldp-local-parent-interface-name><lldp-remote-chassis-id-subtype>Mac address</lldp-remote-chassis-id-subtype><lldp-remote-chassis-id>0c:86:10:7f:8a:00</lldp-remote-chassis-id><lldp-remote-port-description>xe-0/0/8</lldp-remote-port-description><lldp-remote-system-name>R4</lldp-remote-system-name></lldp-neighbor-information><lldp-neighbor-information><lldp-local-port-id>xe-0/0/9</lldp-local-port-id><lldp-local-parent-interface-name>-</lldp-local-parent-interface-name><lldp-remote-chassis-id-subtype>Mac address</lldp-remote-chassis-id-subtype><lldp-remote-chassis-id>0c:86:10:7f:8a:00</lldp-remote-chassis-id><lldp-remote-port-description>xe-0/0/9</lldp-remote-port-description><lldp-remote-system-name>R4</lldp-remote-system-name></lldp-neighbor-information><lldp-neighbor-information><lldp-local-port-id>em0</lldp-local-port-id><lldp-local-parent-interface-name>-</lldp-local-parent-interface-name><lldp-remote-chassis-id-subtype>Mac address</lldp-remote-chassis-id-subtype><lldp-remote-chassis-id>c0:42:d0:24:e0:40</lldp-remote-chassis-id><lldp-remote-port-description>ge-0/0/24.0</lldp-remote-port-description><lldp-remote-system-name>QNC-JTAC-LSW-C15-52</lldp-remote-system-name></lldp-neighbor-information><lldp-neighbor-information><lldp-local-port-id>xe-0/0/2</lldp-local-port-id><lldp-local-parent-interface-name>-</lldp-local-parent-interface-name><lldp-remote-chassis-id-subtype>Mac address</lldp-remote-chassis-id-subtype><lldp-remote-chassis-id>f4:b5:2f:4a:33:00</lldp-remote-chassis-id><lldp-remote-port-description>xe-0/0/2</lldp-remote-port-description><lldp-remote-system-name>R3</lldp-remote-system-name></lldp-neighbor-information></lldp-neighbors-information>

     

     

    Example 2: (customized LLDP neighbor info with a more friendly display)

    --------------

    ####START - Script to get LLDP info from a Juniper device using SSH and Python

    from jnpr.junos import Device
    from lxml import etree
    from jnpr.junos.op.lldp import LLDPNeighborTable

     

    lldp_info = {}
    dev = Device(host="1.1.1.1", user="labroot", password="lab123", port=22, normalize=True)
    dev.open()
    lldp=LLDPNeighborTable(dev)
    lldp.get()
    print ("Local Interface Parent Interface Chassis Id Port info System Name")
    for keys,values in lldp.items():
    lldp_info=values
    if (lldp_info[5] [1] == "em0" or lldp_info[5] [1] == "me0" or lldp_info[5] [1] == "fxp0"):
    print lldp_info[5] [1]," ",lldp_info[0] [1]," ",lldp_info[2] [1]," ",lldp_info[3] [1]," ",lldp_info[6] [1]
    continue
    print lldp_info[5] [1]," ",lldp_info[0] [1]," ",lldp_info[2] [1]," ",lldp_info[3] [1]," ",lldp_info[6] [1]
    print ("\n")
    dev.close()

     

    Device config and CLI output: 

    -------------------------------------

    Same as Example 1

     

    Sample Script Output:

    ------------------------------

    C:\Python27>python lldp_info_basic.py
    Local Interface Parent Interface Chassis Id Port info System Name
    xe-0/0/7 - 0c:86:10:7f:8a:00 xe-0/0/7 R4


    xe-0/0/3 - 0c:86:10:7f:8a:00 xe-0/0/3 R4


    xe-0/0/8 - 0c:86:10:7f:8a:00 xe-0/0/8 R4


    xe-0/0/9 - 0c:86:10:7f:8a:00 xe-0/0/9 R4


    em0 - c0:42:d0:24:e0:40 ge-0/0/24.0 QNC-JTAC-LSW-C15-52
    xe-0/0/2 - f4:b5:2f:4a:33:00 xe-0/0/2 R3

     

     

    #NOTE: Play around with the spaces in quotes in the print statements.  I achieved the above output using  quotes in this order on the three print statements in the Example 2 script:

    first print statement in this order of quotes - 4, 4, 10, 10 spaces after each word
    2nd print statement - 14, 16, 10, 6 spaces after each word
    3rd print statement - 10, 15, 10, 10 spaces

     

    Hope this helps.

     

    Regards,
    -r.

    --------------------------------------------------

    If this solves your problem, please mark this post as "Accepted Solution."
    Kudos are always appreciated :).



  • 3.  RE: Python script of LLDP (Not using Pyezy)

    Posted 02-05-2019 14:35
    Hi sir. Noted and thank you.
    My juniper device environmt does not netconf enable the only way for me to access is traditonl ssh access and netmiko. I havent apply pyez module but i was informed it must be netconf enable device to use it. Do I need netconf to be enabled to run the script above?.


  • 4.  RE: Python script of LLDP (Not using Pyezy)

     
    Posted 02-05-2019 18:19
    Hi jar,

    For the scripts I've shared, I have only enabled SSH on the device.

    Regards,
    -r.

    --------------------------------------------------

    If this solves your problem, please mark this post as "Accepted Solution."
    Kudos are always appreciated :).


  • 5.  RE: Python script of LLDP (Not using Pyezy)

    Posted 02-05-2019 19:23
    Hi sir,
    I though it must be run wth netconf enabled junos devices...if this is not the case..i may test it over my environmt. I will test it and share the finding. Thank you. Will update later.