Skip to content

Instantly share code, notes, and snippets.

@samueljon
Last active April 12, 2018 15:18
Show Gist options
  • Save samueljon/16343186aa89ebf282271d4e0bd2921e to your computer and use it in GitHub Desktop.
Save samueljon/16343186aa89ebf282271d4e0bd2921e to your computer and use it in GitHub Desktop.
Accessing aruba switch via python
#!/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