Thursday, May 29, 2014

python script that will add a vlan on a Juniper switch

Here's a simple python script to add a vlan on an EX switch. I use an argument to pass the vlan id to the script.

from my laptop shell
----------
[MYMAC:~/scripts/PYTHON] user% python vlan_add.py 100
Config added:
set vlans v100 vlan-id 100
diff from junos device:

[edit vlans]
+   v100 {
+       vlan-id 100;
+   }

None
---------------------------
On the EX switch:

{master:0}[edit]
user@myswitch# show vlans 
v100 {
    vlan-id 100;
}

The python script:

[MYMAC:~/scripts/PYTHON] user% more vlan_add.py 
------------------------
from sys import argv
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
session = Device(host='myswitch',user='user',password='password')
session.open()
change = Config(session)
vlan = argv[1]
cmd = 'set vlans v%s vlan-id %s' % (vlan,vlan)
print 'Config added:'
print cmd
change.load(cmd, format ='set')
print 'diff from junos device:'
print change.pdiff()
change.commit()
session.close()

No comments:

Post a Comment