ilom-rest-token.py
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. # example ILOM REST Client using authentication token over several requests # tested with Python 2.7, 3.6 import requests from requests.packages import urllib3 urllib3.disable_warnings(urllib3.exceptions.SubjectAltNameWarning) AUTH_TOKEN_NAME = 'X-Rest-Token' REST_URI = 'https://<SP host or IP>/rest/v1' session = requests.Session() session.verify = '<path to ILOM Certificate' # set to True if you have a CA signed cert session.hooks = { 'response': lambda r, *args, **kwargs: r.raise_for_status() } # login, retrieve auth token, store in session so it's included in all requests response = session.post(REST_URI + '/login', auth=('root', 'changeme')) login = response.json() session.headers.update({AUTH_TOKEN_NAME: login.get(AUTH_TOKEN_NAME)}) try: # ilom command to retrieve version response = session.post(REST_URI + '/SP',json={'op': 'version'}) print ('Version {}'.format(response.json().get('Firmware Version'))) # add a new user Elwood session.post(REST_URI + '/SP/users', json={'user_name': 'Elwood', 'password': 'changeme'}) # delete user elwood session.delete(REST_URI + '/SP/users/Elwood') # set the /SP system_location property to Santa Clara session.patch(REST_URI + '/SP', json={ 'system_location': 'Santa Clara'}) finally: session.post(REST_URI + '/logout')