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 100
Config added:
set vlans v100 vlan-id 100
diff from junos device:

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

On the EX switch:

user@myswitch# show vlans 
v100 {
    vlan-id 100;

The python script:

[MYMAC:~/scripts/PYTHON] user% more 
from sys import argv
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
session = Device(host='myswitch',user='user',password='password')
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()

No comments:

Post a Comment