Created
April 12, 2018 15:19
-
-
Save samueljon/63e65ae893120a39ae8301e83c0e9caf to your computer and use it in GitHub Desktop.
Script to set a VLAN via Aruba's REST Interface.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# | |
import sys, json | |
import httplib, urllib | |
# Check arguments | |
numargs = len(sys.argv) | |
if numargs != 6: | |
print "Usage: aruba-setvlan.py [ip address] [username] [password] [port] [vlan]" | |
sys.exit(1) | |
cmdargs = str(sys.argv) | |
switchIP = str(sys.argv[1]) | |
switchUser = str(sys.argv[2]) | |
switchPass = str(sys.argv[3]) | |
port = (sys.argv[4]) | |
vlan = int(sys.argv[5]) | |
# Connect and authenticate | |
params = json.dumps({"userName":switchUser, "password":switchPass}) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "*/*", "Accept-Encoding": "*"} | |
conn = httplib.HTTPConnection(switchIP) | |
conn.request("POST", "/rest/v1/login-sessions", params, headers) | |
response = conn.getresponse() | |
data = response.read() | |
cookie = json.loads(data)['cookie'] | |
conn.close() | |
params = json.dumps({"vlan_id":vlan, "port_id":port, "port_mode":"POM_UNTAGGED"}) | |
headers = {"Set-Cookie":cookie} | |
conn = httplib.HTTPConnection(switchIP) | |
conn.request("POST", "/rest/v1/vlans-ports", params, headers) | |
data = conn.getresponse() | |
vlanport = json.load(data) | |
print vlanport | |
conn.close() | |
# Connect again, delete cookie | |
params = json.dumps(cookie) | |
headers = {"Set-Cookie":cookie} | |
conn = httplib.HTTPConnection(switchIP) | |
conn.request("DELETE", "/rest/v1/login-sessions", params, headers) | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment