Tuesday, May 27, 2014

My attempt at python scripting using netconf to extract information from a juniper router

Here's a pretty simple script. I loaded all the modules for netconf from github.

You'll need to make sure the router is configured to accept netconf connections.
user@router# show system
services {
    netconf {

Now from my mac I can make an off router connection and extract the Juniper hardware inventory information.

[user-mac:~/scripts/PYTHON] user% python chassis.py
executing command: 'show chassis hardware'

Hardware inventory:
Item             Version  Part number  Serial number     Description
Chassis                                G0004             MX80-P
Midplane         REV 01   711-044315   CAAL7076          MX80-P
PEM 0            Rev 03   740-028288   UE00405           AC Power Entry Module
Routing Engine            BUILTIN      BUILTIN           Routing Engine
TFEB 0                    BUILTIN      BUILTIN           Forwarding Engine Processor
  QXM 0          REV 06   711-028408   CAAL7913          MPC QXM
FPC 0                     BUILTIN      BUILTIN           MPC BUILTIN
  MIC 0                   BUILTIN      BUILTIN           4x 10GE XFP
    PIC 0                 BUILTIN      BUILTIN           4x 10GE XFP
      Xcvr 1     REV 01   740-014289   T08A16369         XFP-10G-SR
      Xcvr 3     REV 03   740-014289   CA06BQ03L         XFP-10G-SR
FPC 1                     BUILTIN      BUILTIN           MPC BUILTIN
  MIC 0          REV 26   750-028392   CAAH5493          3D 20x 1GE(LAN) SFP
    PIC 0                 BUILTIN      BUILTIN           10x 1GE(LAN) SFP
      Xcvr 0     REV 02   740-014132   PNC0ZYF           SFP-T
      Xcvr 5     REV 01   740-011782   PCK067K           SFP-SX
    PIC 1                 BUILTIN      BUILTIN           10x 1GE(LAN) SFP
      Xcvr 0     REV 02   740-014132   PNC0ZXX           SFP-T
      Xcvr 1     REV 01   740-013111   6500192           SFP-T
      Xcvr 3     REV 01   740-014132   72322029          SFP-T
      Xcvr 5     REV 01   740-013111   70851030          SFP-T
      Xcvr 8     REV 01   740-014132   72331007          SFP-T
Fan Tray                                                 Fan Tray

Actual Python script.

[user-mac:~/scripts/PYTHON] user% more chassis.py 

from pprint import pprint as pp 
from lxml import etree

from jnpr.junos import Device as Junos

login = dict(user='user', host='', password='password')
jdev = Junos(**login)

# you can run any cli command using the :cli: method, for example
print "executing command: 'show chassis hardware'"
print jdev.cli("show chassis hardware")

No comments:

Post a Comment