File: get_l2vpns.py

package info (click to toggle)
python-ncclient 0.6.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: python: 9,548; xml: 476; makefile: 77; sh: 5
file content (54 lines) | stat: -rw-r--r-- 1,976 bytes parent folder | download
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
#!/usr/bin/python3
import logging

from ncclient import manager
from ncclient.xml_ import *


def connect(host, port, user, password):
    with manager.connect(
        host=host,
        port=port,
        username=user,
        password=password,
        timeout=60,
        device_params={'name': 'junos'},
        hostkey_verify=False
    ) as conn:
        vpns = conn.command(
            'show l2circuit connections'
        ).xpath(
            'l2circuit-connection-information/l2circuit-neighbor'
        )
        connection_dict = {}
        for vpn in vpns:
            neighbor = ''
            for tag in vpn.getchildren():
                connection_details_dict = {}
                if tag.tag == 'neighbor-address':
                    neighbor = tag.text
                if tag.tag == 'connection':
                    for child in tag.getchildren():
                        if child.tag == 'local-interface':
                            ifce = child.find('interface-name').text
                            connection_details_dict.update({'interface': ifce})
                        else:
                            if child.tag == 'connection-id':
                                cid = child.text
                            else:
                                connection_details_dict.update({child.tag: child.text})
                    connection_details_dict.update({'neighbor': neighbor})
                    if connection_dict.get(cid):
                        connection_dict.get(cid).append(connection_details_dict)
                    else:
                        connection_dict.update({cid: [connection_details_dict]})

    return connection_dict


if __name__ == '__main__':
    LOG_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
    logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=LOG_FORMAT)

    response = connect('router', 22, 'netconf', 'juniper!')
    logging.info("Response: %s", response)