summaryrefslogtreecommitdiff
path: root/src/op_mode/lldp_op.py
blob: 4d8fdbc99e9704832f307ece5cb964ec5cb8aa64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
#
# Copyright (C) 2019 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import argparse
import jinja2

from xml.dom import minidom
from sys import exit
from subprocess import Popen, PIPE, STDOUT
from tabulate import tabulate

parser = argparse.ArgumentParser()
parser.add_argument("-a", "--all", action="store_true", help="Show LLDP neighbors on all interfaces")
parser.add_argument("-i", "--interface", action="store", help="Show LLDP neighbors on specific interface")

# Please be careful if you edit the template.
lldp_out = """Capability Codes: R - Router, B - Bridge, W - Wlan r - Repeater, S - Station
                  D - Docsis, T - Telephone, O - Other

Device ID                 Local     Proto  Cap   Platform             Port ID
---------                 -----     -----  ---   --------             -------
{% for n in neighbors -%}
{{ "%-25s" | format(n.chassis) }} {{ "%-9s" | format(n.interface) }} {{ "%-6s" | format(n.proto) }} {{ "%-5s" | format(n.cap) }} {{ "%-20s" | format(n.platform) }} {{ n.port }}
{% endfor -%}
"""

def _get_neighbors():
    command = '/usr/sbin/lldpcli -f xml show neighbors'
    p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
    tmp = p.communicate()[0].strip()
    return tmp.decode()

def extract_neighbor(neighbor):
    """
    Extract LLDP neighbor information from XML document passed as param neighbor

    <lldp>
     <interface label="Interface" name="eth0" via="LLDP" rid="3" age="0 day, 00:17:42">
      <chassis label="Chassis">
       <id label="ChassisID" type="mac">00:50:56:9d:a6:11</id>
       <name label="SysName">VyOS</name>
       <descr label="SysDescr">VyOS unknown</descr>
       <mgmt-ip label="MgmtIP">172.18.254.203</mgmt-ip>
       <mgmt-ip label="MgmtIP">fe80::250:56ff:fe9d:a611</mgmt-ip>
       <capability label="Capability" type="Bridge" enabled="off"/>
       <capability label="Capability" type="Router" enabled="on"/>
       <capability label="Capability" type="Wlan" enabled="off"/>
       <capability label="Capability" type="Station" enabled="off"/>
      </chassis>
      <port label="Port">
       <id label="PortID" type="mac">00:50:56:9d:a6:11</id>
       <descr label="PortDescr">eth0</descr>
       <ttl label="TTL">120</ttl>
       <auto-negotiation label="PMD autoneg" supported="no" enabled="no">
        <current label="MAU oper type">10GigBaseCX4 - X copper over 8 pair 100-Ohm balanced cable</current>
       </auto-negotiation>
      </port>
      <vlan label="VLAN" vlan-id="203">eth0.203</vlan>
      <lldp-med label="LLDP-MED">
       <device-type label="Device Type">Network Connectivity Device</device-type>
       <capability label="Capability" type="Capabilities" available="yes"/>
       <capability label="Capability" type="Policy" available="yes"/>
       <capability label="Capability" type="Location" available="yes"/>
       <capability label="Capability" type="MDI/PSE" available="yes"/>
       <capability label="Capability" type="MDI/PD" available="yes"/>
       <capability label="Capability" type="Inventory" available="yes"/>
       <inventory label="Inventory">
        <hardware label="Hardware Revision">None</hardware>
        <software label="Software Revision">4.19.54-amd64-vyos</software>
        <firmware label="Firmware Revision">6.00</firmware>
        <serial label="Serial Number">VMware-42 1d cf 87 ab 7f da 7e-3</serial>
        <manufacturer label="Manufacturer">VMware, Inc.</manufacturer>
        <model label="Model">VMware Virtual Platform</model>
        <asset label="Asset ID">No Asset Tag</asset>
       </inventory>
      </lldp-med>
     </interface>
    </lldp>
    """

    device = {
        'interface' : neighbor.getAttribute('name'),
        'chassis' : '',
        'proto' : neighbor.getAttribute('via'),
        'descr' : '',
        'cap' : '',
        'platform' : '',
        'port' : ''
    }

    # first change to <chassis> node and then retrieve <name> and <descr>
    chassis = neighbor.getElementsByTagName('chassis')
    device['chassis'] = chassis[0].getElementsByTagName('name')[0].firstChild.data
    # Cisco IOS comes with a ',' remove character ....
    device['platform'] = chassis[0].getElementsByTagName('descr')[0].firstChild.data[:20].replace(',',' ')

    # extract capabilities
    for capability in chassis[0].getElementsByTagName('capability'):
        # we are only interested in enabled capabilities ...
        if capability.getAttribute('enabled') == "on":
            if capability.getAttribute('type') == "Router":
                device['cap'] += 'R'
            elif capability.getAttribute('type') == "Bridge":
                device['cap'] += 'B'
            elif capability.getAttribute('type') == "Wlan":
                device['cap'] += 'W'
            elif capability.getAttribute('type') == "Station":
                device['cap'] += 'S'
            elif capability.getAttribute('type') == "Repeater":
                device['cap'] += 'r'
            elif capability.getAttribute('type') == "Telephone":
                device['cap'] += 'T'
            elif capability.getAttribute('type') == "Docsis":
                device['cap'] += 'D'
            elif capability.getAttribute('type') == "Other":
                device['cap'] += 'O'

    # first change to <port> node and then retrieve <descr>
    port = neighbor.getElementsByTagName('port')
    port = port[0].getElementsByTagName('descr')[0].firstChild.data
    device['port'] = port


    return device

if __name__ == '__main__':
    args = parser.parse_args()
    tmp = { 'neighbors' : [] }

    if args.all:
       neighbors = minidom.parseString(_get_neighbors())
       for neighbor in neighbors.getElementsByTagName('interface'):
           tmp['neighbors'].append( extract_neighbor(neighbor) )

    elif args.interface:
        neighbors = minidom.parseString(_get_neighbors())
        for neighbor in neighbors.getElementsByTagName('interface'):
            # check if neighbor appeared on proper interface
            if neighbor.getAttribute('name') == args.interface:
                tmp['neighbors'].append( extract_neighbor(neighbor) )

    else:
        parser.print_help()
        exit(1)

    tmpl = jinja2.Template(lldp_out)
    config_text = tmpl.render(tmp)
    print(config_text)

    exit(0)