Skip to content

Juniper Library

svc_juniper_lib.juniper

juniper_get_ex2200_version(ip_address, username, password)

Return the software version string reported by an EX2200 device.

The function extracts the version substring contained in square brackets, if present.

Parameters

ip_address : str Hostname or IP of the EX2200 device. username : str Username for device authentication. password : str Password for device authentication.

Returns

str Extracted version string (contents inside brackets if present) or the raw version string.

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
def juniper_get_ex2200_version(ip_address,username,password):
    """Return the software version string reported by an EX2200 device.

    The function extracts the version substring contained in square brackets, if present.

    Parameters
    ----------
    ip_address : str
        Hostname or IP of the EX2200 device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    str
        Extracted version string (contents inside brackets if present) or the raw version string.
    """
    dev = Device(host=ip_address, user=username, password=password, port='22',timeout=300).open()
    ex_version = EX2200Version(dev)
    ex_version.get()

    results=ex_version[0].version
    #extract only the version
    only_version = results[results.find('[')+1:results.find(']')]
    return only_version

juniper_get_ex3400_version(fqdn, username, password)

Return the software version string reported by an EX3400 device.

Parameters

fqdn : str Hostname or IP of the EX3400 device. username : str Username for device authentication. password : str Password for device authentication.

Returns

str Version string as returned by the EX3400 device model.

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
def juniper_get_ex3400_version(fqdn, username, password):
    """Return the software version string reported by an EX3400 device.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the EX3400 device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    str
        Version string as returned by the EX3400 device model.
    """
    dev = Device(host=fqdn, user=username, password=password, port='22',timeout=300).open()
    ex_version = EX3400Version(dev)
    ex_version.get()

    results=ex_version[0].version
    return results

juniper_get_ex_interfaces(fqdn, username, password)

Return EX/QFX-EX interface metadata including description, speed, and fiber/copper type.

Parameters

fqdn : str Hostname or IP of the EX device. username : str Username for device authentication. password : str Password for device authentication.

Returns

dict[str, dict] Mapping of interface name to a dict containing: - 'description' (str) - 'speed' (str) - 'type' (str) one of 'SMF', 'MMF', 'copper', or 'lag'

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
def juniper_get_ex_interfaces(fqdn, username, password):
    """Return EX/QFX-EX interface metadata including description, speed, and fiber/copper type.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the EX device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    dict[str, dict]
        Mapping of interface name to a dict containing:
        - 'description' (str)
        - 'speed' (str)
        - 'type' (str) one of 'SMF', 'MMF', 'copper', or 'lag'
    """
    # Netconf session to a juniper device
    with Device(host=fqdn, user=username, password=password, port='22',timeout=300) as dev:
        ports = QFXEXPhysicalTable(dev)
        sfp = QFXEXChassisHardware(dev)
        ports.get()
        sfp.get()

    # create a dictionary of interface names with another dictionary with port description, sfp type and speed
    results = {}
    for key,value in ports.items():
        if value[0][1] == None:
            results.update({key: {'description': ''}})
        else:
            results.update({key:{'description':value[0][1]}})
        #change 1000mbps to 1Gbps to match Netbox tag
        if value[1][1] == '1000mbps' or value[1][1] == '1000 Mbps' or value[1][1] == 'Auto':
            results[key]['speed'] = '1Gbps'
            results[key]['type'] = 'copper'
        elif value[1][1] == '100mbps':
            results[key]['speed'] = '100mbps'
            results[key]['type'] = 'copper'
        else:
            results[key]['speed'] = value[1][1]

        # for ae interfaces set type to lag
        if 'ae' in key:
            results[key]['type'] = 'lag'

    for key,value in sfp.items():
        #construct interface from fpc, pic, port and sfp description
        if '10G' in value[1][1]:
            type = 'xe-'
        else:
            type = 'ge-'
        fpc = value[4][1].replace('FPC ', '')
        pic = value[3][1].replace('PIC ', '')
        port = key.replace('Xcvr ','')

        #match chassis hardware description to netbox tags
        if int(port) < 48:
            try:
                if value[1][1] == 'SFP+-10G-LR' or value[1][1] == 'SFP-LX10' or value[1][1] == 'QSFP+-40G-LR4':
                    results[type+fpc+'/'+pic+'/'+port]['type']='SMF'
                elif value[1][1] == 'SFP+-10G-SR' or value[1][1] == 'SFP-SX':
                    results[type + fpc + '/' + pic + '/' + port]['type'] = 'MMF'
                else:
                    results[type + fpc + '/' + pic + '/' + port]['type'] = 'copper'
            except:
                pass

    return results

juniper_get_instance(fqdn, site, username, password)

Retrieve routing-instance (VRF) information from an MX device.

Parameters

fqdn : str Hostname or IP of the MX device. site : str Site label used when constructing certain route-distinguisher strings for special instances. username : str Username for device authentication. password : str Password for device authentication.

Returns

dict[str, dict] Mapping of routing-instance name to a dict with keys: - 'instance_type' (str) - 'route_distinguisher' (str or None) - 'instance_interface' (list[str])

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
def juniper_get_instance(fqdn, site, username, password):
    """Retrieve routing-instance (VRF) information from an MX device.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the MX device.
    site : str
        Site label used when constructing certain route-distinguisher strings for special instances.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    dict[str, dict]
        Mapping of routing-instance name to a dict with keys:
        - 'instance_type' (str)
        - 'route_distinguisher' (str or None)
        - 'instance_interface' (list[str])
    """
    # Netconf session to a juniper device
    with Device(host=fqdn, user=username, password=password, port='22',timeout=300) as dev:
        instance=MXRouteInstance(dev)
        instance.get()

    results={}
    for key,value in instance.items():
        if '__' not in key and 'master' not in key and 'junos' not in key:
            if value[1][1]=='0:0':
                results.update({key: {'instance_type': value[0][1], 'route_distinguisher': None,
                                  'instance_interface': value[2][1]}})
            elif key == "RI-VRF-Internet-2":
                if value[1][1] == None:
                    results.update({site.upper() + ' ' + key: {'instance_type': value[0][1], 'route_distinguisher':value[1][1],
                                                                   'instance_interface': value[2][1]}})
                else:
                    results.update({site.upper()+' '+key: {'instance_type': value[0][1], 'route_distinguisher': site +' '+value[1][1],
                                                               'instance_interface': value[2][1]}})

            else:
                if value[1][1] == None:
                    results.update({key: {'instance_type': value[0][1], 'route_distinguisher': value[1][1],
                                          'instance_interface': value[2][1]}})
                else:
                    results.update(
                        {key: {'instance_type': value[0][1], 'route_distinguisher': site + ' ' + value[1][1],
                               'instance_interface': value[2][1]}})

    return results

juniper_get_mx_interface_vlans_dictionary(fqdn, username, password)

Return a mapping of MX subinterface VLAN IDs to their configured descriptions.

Parameters

fqdn : str Hostname or IP of the MX device. username : str Username for device authentication. password : str Password for device authentication.

Returns

dict[int, str] Mapping of VLAN ID to description. If an interface has no description the value will be 'None'.

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
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
def juniper_get_mx_interface_vlans_dictionary(fqdn, username, password):
    """Return a mapping of MX subinterface VLAN IDs to their configured descriptions.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the MX device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    dict[int, str]
        Mapping of VLAN ID to description. If an interface has no description the value will be 'None'.
    """
    # Netconf session to a juniper device
    with Device(host=fqdn, user=username, password=password, port='22', timeout=300) as dev:
        ports = MXLogicalTable(dev)
        ports.get()

    # create a dictionary of subinterfaces/vlans (key) and the description (value)
    results = {}

    for key, value in ports.items():
        subinterface = key.split('.')

        if 'xe' in subinterface[0] or 'ge' in subinterface[0] or 'ae' in subinterface[0] or 'ms' in subinterface[0]:
            if 1 < int(subinterface[1]) < 4095:
                if value[0][1]:
                    results.update({int(subinterface[1]): value[0][1]})
                else:
                    results.update({int(subinterface[1]): 'None'})

    return results

juniper_get_mx_interfaces(fqdn, username, password)

Return MX interface metadata including description, speed, and fiber/copper type.

Parameters

fqdn : str Hostname or IP of the MX device. username : str Username for device authentication. password : str Password for device authentication.

Returns

dict[str, dict] Mapping of interface name to a dict containing: - 'description' (str) - 'speed' (str) - 'type' (str) one of 'SMF', 'MMF', 'copper', 'lag', or 'No SFP'

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def juniper_get_mx_interfaces(fqdn, username, password):
    """Return MX interface metadata including description, speed, and fiber/copper type.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the MX device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    dict[str, dict]
        Mapping of interface name to a dict containing:
        - 'description' (str)
        - 'speed' (str)
        - 'type' (str) one of 'SMF', 'MMF', 'copper', 'lag', or 'No SFP'
    """
    # Netconf session to a juniper device
    with Device(host=fqdn, user=username, password=password, port='22',timeout=300) as dev:
        ports = MXPhysicalTable(dev)
        sfp = MXChassisHardware(dev)
        ports.get()
        sfp.get()
    # create a dictionary of interface names with another dictionary with port description, sfp type and speed
    results = {}
    for key, value in ports.items():
        if value[0][1] is None:
            results.update({key: {'description': ''}})
        else:
            results.update({key: {'description': value[0][1]}})

        # change 1000mbps to 1Gbps to match Netbox tag
        if value[1][1] == '1000mbps' or value[1][1] == '1000 Mbps':
            results[key]['speed'] = '1Gbps'
            results[key]['type'] = 'copper'
        else:
            results[key]['speed'] = value[1][1]

        # for ae interfaces set type to lag, anything else set to None
        if 'ae' in key:
            results[key]['type'] = 'lag'
        elif 'ge' in key:
            results[key]['type'] = 'copper'
        else:
            results[key]['type'] = 'No SFP'

    for key,value in sfp.items():
        #construct interface from fpc, pic, port and sfp description
        if '10G' in value[1][1]:
            type = 'xe-'
        else:
            type = 'ge-'
        fpc = value[4][1].replace('FPC ', '')
        pic = value[2][1].replace('PIC ', '')
        port = key.replace('Xcvr ', '')

        #match chassis hardware description to netbox tags
        try:
            if value[1][1] == 'SFP+-10G-LR' or value[1][1] == 'SFP-LX10' or value[1][1] == 'QSFP+-40G-LR4' or value[1][1] == 'XFP-10G-LR':
                results[type+fpc+'/'+pic+'/'+port]['type']='SMF'
            elif value[1][1] == 'SFP+-10G-SR' or value[1][1] == 'SFP-SX':
                results[type + fpc + '/' + pic + '/' + port]['type'] = 'MMF'
            else:
                results[type + fpc + '/' + pic + '/' + port]['type'] = 'copper'
        except:
            pass
    return results

juniper_get_mx_ipv4_public_routes(fqdn, site, username, password)

Return public IPv4 networks configured at a specific SVC site on an MX device.

The function selects a site-specific route parser (from built-in YML models) to get public networks and then maps each route to the interface description from the MX logical table.

Parameters

fqdn : str Hostname or IP of the MX device. site : str Site identifier used to select the correct route parser (e.g. 'at1', 'ch3', 'ny5', etc.). username : str Username for device authentication. password : str Password for device authentication.

Returns

dict[str, str] Mapping of route (CIDR string) to the interface description (value).

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
def juniper_get_mx_ipv4_public_routes(fqdn,site,username,password):
    """Return public IPv4 networks configured at a specific SVC site on an MX device.

    The function selects a site-specific route parser (from built-in YML models) to get public
    networks and then maps each route to the interface description from the MX logical table.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the MX device.
    site : str
        Site identifier used to select the correct route parser (e.g. 'at1', 'ch3', 'ny5', etc.).
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    dict[str, str]
        Mapping of route (CIDR string) to the interface description (value).
    """
    # Netconf session to a juniper device
    with Device(host=fqdn, user=username, password=password, port='22', timeout=300) as dev:
        if site =='at1':
            routes = br1svcat1corpequinixcom(dev)
        elif site =='ch3':
            routes = br1svcch3corpequinixcom(dev)
        elif site =='da6':
            routes = br1svcda6corpequinixcom(dev)
        elif site =='dc6':
            routes = br1svcdc6corpequinixcom(dev)
        elif site =='la3':
            routes = br1svcla3corpequinixcom(dev)
        elif site =='mi1':
            routes = br1svcmi1corpequinixcom(dev)
        elif site =='ny5':
            routes = br1svcny5corpequinixcom(dev)
        elif site =='se3':
            routes = br1svcse3corpequinixcom(dev)
        elif site =='sv5':
            routes = br1svcsv5corpequinixcom(dev)
        elif site =='am3':
            routes = svcbr1am3corpeuequinixcom(dev)
        elif site =='fr4':
            routes = svcbr1fr4corpeuequinixcom(dev)
        elif site =='ld5':
            routes = svcbr1ld5corpeuequinixcom(dev)
        elif site =='hk2':
            routes = br1svchk2apequinixcom(dev)
        elif site =='os1':
            routes = br1svcos1apequinixcom(dev)
        elif site =='sg2':
            routes = br1svcsg2apequinixcom(dev)
        elif site =='sy4':
            routes = br1svcsy4apequinixcom(dev)
        elif site =='ty4':
            routes = br1svcty4apequinixcom(dev)
        elif site =='tr2':
            routes = br1svctr2corpequinixcom(dev)
        routes.get()

        ports = MXLogicalTable(dev)
        ports.get()

    # create a dictionary of routes and descriptions (based on interface description)
    results = {}
    for key, value in routes.items():
        if value[3][1] is None and value[4][1] is None:
            if value[2][1] in ports:
                results.update({key: ports[value[2][1]]['description']})
    return results

juniper_get_mx_version(fqdn, username, password)

Return the software version string reported by a Juniper MX device.

Parameters

fqdn : str Hostname or IP of the MX device. username : str Username for device authentication. password : str Password for device authentication.

Returns

str Version string as returned by the MX device model.

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
def juniper_get_mx_version(fqdn, username, password):
    """Return the software version string reported by a Juniper MX device.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the MX device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    str
        Version string as returned by the MX device model.
    """
    dev = Device(host=fqdn, user=username, password=password, port='22',timeout=300).open()
    mx_version = MXVersion(dev)
    mx_version.get()

    results=mx_version[0].version
    return results

juniper_get_qfx_interfaces(fqdn, username, password)

Return QFX interface metadata including description, speed, and fiber/copper type.

Parameters

fqdn : str Hostname or IP of the QFX device. username : str Username for device authentication. password : str Password for device authentication.

Returns

dict[str, dict] Mapping of interface name to a dict containing: - 'description' (str) - 'speed' (str) e.g. '1Gbps', '10Gbps' or 'None' - 'type' (str) one of 'SMF', 'MMF', 'copper', or 'lag'

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def juniper_get_qfx_interfaces(fqdn, username, password):
    """Return QFX interface metadata including description, speed, and fiber/copper type.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the QFX device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    dict[str, dict]
        Mapping of interface name to a dict containing:
        - 'description' (str)
        - 'speed' (str) e.g. '1Gbps', '10Gbps' or 'None'
        - 'type' (str) one of 'SMF', 'MMF', 'copper', or 'lag'
    """
    # Netconf session to a juniper device
    with Device(host=fqdn, user=username, password=password, port='22', timeout=300) as dev:
        phy_port = QFXEXPhysicalTable(dev)
        sfp_info = QFXEXChassisHardware(dev)
        phy_port.get()
        sfp_info.get()

    # create a dictionary of interface names with another dictionary with port description, sfp type and speed
    results = {}
    for key, value in phy_port.items():
        if value[0][1] == None:
            results.update({key: {'description': ''}})
        else:
            results.update({key: {'description': value[0][1]}})
        # change 1000mbps to 1Gbps to match Netbox tag
        if value[1][1] == '1000mbps' or value[1][1] == '1000 Mbps':
            results[key]['speed'] = '1Gbps'
        elif value[1][1] == 'Auto':
            results[key]['speed'] = '10Gbps'
        elif value[1][1] == None:
            results[key]['speed'] = 'None'
        else:
            results[key]['speed'] = value[1][1]

        # for em interfaces set type to copper
        if 'em' in key:
            results[key]['type'] = 'copper'
        elif 'ae' in key:
            results[key]['type'] = 'lag'
    for key, value in sfp_info.items():
        # construct interface from fpc, pic, port and sfp description
        if '10G' in value[1][1]:
            type = 'xe-'
        else:
            type = 'ge-'
        fpc = value[4][1].replace('FPC ', '')
        pic = value[3][1].replace('PIC ', '')
        port = key.replace('Xcvr ', '')

        # match chassis hardware description to netbox tags
        if int(port) < 48:
            try:
                if value[1][1] == 'SFP+-10G-LR' or value[1][1] == 'SFP-LX10' or value[1][1] == 'QSFP+-40G-LR4':
                    results[type+fpc+'/'+pic+'/'+port]['type'] = 'SMF'
                elif value[1][1] == 'SFP+-10G-SR' or value[1][1] == 'SFP-SX':
                    results[type + fpc + '/' + pic + '/' + port]['type'] = 'MMF'
                else:
                    results[type + fpc + '/' + pic + '/' + port]['type'] = 'copper'
            except:
                pass
    return results

juniper_get_qfx_version(fqdn, username, password)

Return the software version string reported by a Juniper QFX device.

Parameters

fqdn : str Hostname or IP of the QFX device. username : str Username for device authentication. password : str Password for device authentication.

Returns

str Version string as returned by the QFX device model.

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
def juniper_get_qfx_version(fqdn, username, password):
    """Return the software version string reported by a Juniper QFX device.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the QFX device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    str
        Version string as returned by the QFX device model.
    """
    dev = Device(host=fqdn, user=username, password=password, port='22',timeout=300).open()
    qfx_version = QFXVersion(dev)
    qfx_version.get()

    results=qfx_version[0].version
    return results

juniper_get_qfx_vlans_dictionary(fqdn, username, password)

Return a mapping of VLAN IDs to configured VLAN names on a QFX device.

Parameters

fqdn : str Hostname or IP of the QFX device. username : str Username for device authentication. password : str Password for device authentication.

Returns

dict[int, str] Mapping of VLAN tag (int) to VLAN name (str).

Source code in packages/svc_juniper_lib/src/svc_juniper_lib/juniper.py
 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
def juniper_get_qfx_vlans_dictionary(fqdn, username, password):
    """Return a mapping of VLAN IDs to configured VLAN names on a QFX device.

    Parameters
    ----------
    fqdn : str
        Hostname or IP of the QFX device.
    username : str
        Username for device authentication.
    password : str
        Password for device authentication.

    Returns
    -------
    dict[int, str]
        Mapping of VLAN tag (int) to VLAN name (str).
    """
    # Netconf session to a juniper device
    with Device(host=fqdn, user=username, password=password, port='22', timeout=300) as dev:
        vlans = QFXVlanTable(dev)
        vlans.get()

    # create a dictionary of vlan tags (key) and vlan names (value)
    results = {}

    for key, value in vlans.items():
        results.update({int(key): value[0][1]})

    return results