Monday, June 16, 2014

Python script - make a config change to a Juniper device remotely

I had a previous blog post on how to make a config change remotely using python. This new script uses a more structured framework from python-ez. I will be making calls to the following module:

% pwd
/Library/Python/2.7/site-packages/jnpr/junos/cfg/phyport

The base module supports the following methods:

    PROPERTIES = [
        'admin',              # True
        'description',        # str
        'speed',              # ['10m','100m','1g','10g']
        'duplex',             # ['full','half']
        'mtu',                # int
        'loopback',           # True
        '$unit_count'         # number of units defined
    ]

With python I can iterate through all the interfaces and change the mtu size on all the interfaces

before the change:

jnpr@R1# show interfaces
xe-0/0/0 {
    mtu 1500;
}
xe-0/0/1 {
    mtu 1500;
}
xe-0/0/2 {
    mtu 1500;
}
xe-0/0/3 {
    mtu 1500;
}

% python mtu-chg.py
Host: 10.161.33.171
Model: MX80-P
Version: 12.3R1.7
Changing MTU to 9000 on the following interfaces:
xe-0/0/0
xe-0/0/1
xe-0/0/2
xe-0/0/3

after

jnpr@R1# show interfaces   
xe-0/0/0 {
    mtu 9000;
}
xe-0/0/1 {
    mtu 9000;
}
xe-0/0/2 {
    mtu 9000;
}
xe-0/0/3 {
    mtu 9000;
}

SOURCE CODE
------------

from jnpr.junos import Device as Junos
from jnpr.junos.cfg.phyport import *

login = dict(user='jnpr', host='10.161.33.171', password='pass123')

rtr = Junos(**login)

rtr.open()

print "Host: " + rtr.hostname
print "Model: " + rtr.facts['model']
print "Version: " + rtr.facts['version']
size = 9000

ints = PhyPort(rtr)
print "Changing MTU to %s on the following interfaces:" % size
for int in ints:
  print int.name
  int['mtu'] = size
  int.write()

rtr.close()

No comments:

Post a Comment