Today a short script that shows how to get a authorization token from devices with a REST API and use it to change a config. The example device is an Avocent 8032.
import requests
from requests.structures import CaseInsensitiveDict
urls = ["https://1.1.1.1"]
for url in urls:
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
r = requests.post(url + ':48048/api/v1/sessions/login', verify=False, json={"username": "<yourusername","password": "<yourpassword>"}, headers=headers)
json_response = r.json()
mytoken = json_response["token"]
headers = CaseInsensitiveDict()
tokenstring = str(mytoken)
#GETTING SYSTEM INFO
result = requests.get(url + ':48048/api/v1/system/info', verify=False, headers={'Content-Type':'application/json', 'Authorization': 'Bearer {}'.format(tokenstring)})
systeminfo_response = result.json()
print(systeminfo_response)
#CHANGING_IDLE_TIMEOUT
result = requests.put(url + ':48048/api/v1/security', verify=False, json={'idleTimeout': 3600}, headers={'Content-Type':'application/json', 'Authorization': 'Bearer {}'.format(tokenstring)})
print(result)
…and Bob’s your uncle!
Prerequisites:
API needs to be enabled: SSH into your device and change the parameter
enable_api_https_access = yes
The path to the parameter is under path /system/security/security_profile
The code itself is pretty self-explanatory. Any json data you want to send (=modify values) must be inside the json body. verify=False is used to bypass self-signed certificate warning. Once you have the token, you need to include it in later requests as an auth header. Once the PUT request is sent, the expected response is 204 or 200.
API of Avocent is described at https://www.vertiv.com/4a7004/globalassets/shared/avocent-acs8008000-application-programming-interface_0.pdf
import requests, json, urllib3
from requests.api import post
#disabling ssl warnings
urllib3.disable_warnings()
base_url = „https://1.1.1.1”
login_url = ‚:48048/api/v1/sessions/login’
sysinfo_url= ‚:48048/api/v1/system/info’
sec_url =’:48048/api/v1/security’
headers = {‚Content-type’: ‚application/json’, ‚Accept’: ‚application/json’}
user = „yourusername”
pw = „yourpassword”
idle_tout={‚idleTimeout’: 3600}
#GETTING TOKEN
token = requests.post(f'{base_url}{login_url}’, headers=headers, json={„username”: user, „password”: pw}, verify=False).json()[‚token’]
headers[„token”]=token
#GETTING SYSTEM INFO
systeminfo = requests.get(f'{base_url}{sysinfo_url}’, headers=headers, verify=False).json()
print(json.dumps(systeminfo, indent=2))
#CHANGING_IDLE_TIMEOUT
result = requests.put(f'{base_url}{sec_url}’, json=idle_tout, headers=headers, verify=False).json()
print(json.dumps(result, indent=2))
PolubieniePolubienie
Small improovment and correction of token assignment 😉
import requests, json, urllib3
#disabling ssl warnings
urllib3.disable_warnings()
base_url = „https://1.1.1.1:48048/api/v1”
login_url = ‚/sessions/login’
sysinfo_url= ‚/system/info’
sec_url =’/security’
headers = {‚Content-type’: ‚application/json’, ‚Accept’: ‚application/json’}
creds = {„username”: ‚yourusername’, „password”: ‚yourpassword’}
idle_tout={‚idleTimeout’: 3600}
#GETTING TOKEN
token = requests.post(f'{base_url}{login_url}’, headers=headers, json=creds, verify=False).json()[‚token’]
headers[„Authorization”]=f’Bearer{token}’
#GETTING SYSTEM INFO
systeminfo = requests.get(f'{base_url}{sysinfo_url}’, headers=headers, verify=False).json()
print(json.dumps(systeminfo, indent=2))
#CHANGING_IDLE_TIMEOUT
result = requests.put(f'{base_url}{sec_url}’, json=idle_tout, headers=headers, verify=False).json()
print(json.dumps(result, indent=2))
PolubieniePolubienie