File: add-vlan.py

package info (click to toggle)
snimpy 0.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 484 kB
  • ctags: 435
  • sloc: python: 3,361; makefile: 6
file content (40 lines) | stat: -rw-r--r-- 1,267 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/snimpy

"""
On Nortel switches, create a new VLAN and tag it on "TagAll" ports.
"""

from __future__ import print_function

import os
import sys

load("SNMPv2-MIB")
load(os.path.expanduser("~/.snmp/mibs/RAPID-CITY-MIB"))
load(os.path.expanduser("~/.snmp/mibs/RC-VLAN-MIB"))

vlanNumber = int(sys.argv[3])
vlanName = sys.argv[4]
s = M(host=sys.argv[1], community=sys.argv[2])

# Create the VLAN
if vlanNumber not in s.rcVlanId:
    print("VLAN %d will be created with name %s on %s" % (vlanNumber, vlanName, sys.argv[1]))
    with s:
        s.rcVlanRowStatus[vlanNumber] = "createAndGo"
        s.rcVlanName[vlanNumber] = vlanName
        s.rcVlanType[vlanNumber] = "byPort"
else:
    print("VLAN %d already exists on %s" % (vlanNumber, sys.argv[1]))
    # Just set the name
    if s.rcVlanName[vlanNumber] != vlanName:
        s.rcVlanName[vlanNumber] = vlanName

# Which ports are tagall ?
tagged = [port for port in s.rcVlanPortPerformTagging
          if s.rcVlanPortPerformTagging[port] ]
if len(tagged) != 2 and len(tagged) != 3:
    print("%s does not have exactly two or three tagged ports (%r)" % (sys.argv[1], tagged))
    sys.exit(1)
print("VLAN %d will be tagged on ports %s" % (vlanNumber, tagged))
s.rcVlanStaticMembers[vlanNumber] |= tagged