'create-guest-user.py' Source Code

#!/usr/bin/env python

###########################################################################
#                                                                         #
# This script demonstrates how to create guest user through ISE ERS Guest #
# users API  by executing a Python script.                                #
#                                                                         #
# SECURITY WARNING - DO NOT USE THIS SCRIPT IN PRODUCTION!                #
# The script allows connections to SSL sites without trusting             #
# the server certificates.                                                #
# For production, it is required to add certificate check.                #
#                                                                         #
# Usage: create-guest-user.py <ISE host><Sponsor user><Sponsor password>  #
# <portalId><username><firstName><lastName><password><email><validDays>   #
# <fromDate><toDate>                                                      #
###########################################################################

import http.client
import base64
import ssl
import sys

# parameters
portalId = sys.argv[4]  # "ce91b520-bb67-11e6-b562-005056873bd0"
userName = sys.argv[5]  # "chris"
firstName = sys.argv[6]  # "Chris"
lastName = sys.argv[7]  # "Colombus"
password = sys.argv[8]  # "Password1"
emailAddress = sys.argv[9]  # "chris@cisco.com"
validDays = sys.argv[10]  # "90"
fromDate = sys.argv[11]  # "10/07/2016"
toDate = sys.argv[12]  # "01/04/2017"

# host and authentication credentials
host = sys.argv[1] # "10.20.30.40"
user = sys.argv[2] # "sponsor"
password = sys.argv[3] # "Password1"


conn = http.client.HTTPSConnection("{}:9060".format(host), context=ssl.SSLContext(ssl.PROTOCOL_TLSv1))

creds = str.encode(':'.join((user, password)))
encodedAuth = bytes.decode(base64.b64encode(creds))

req_body_json = """  {{
       "GuestUser" : {{
        "guestType": "Contractor (default)",
        "reasonForVisit": "Business meetng",
        "portalId": "{}",
        "guestInfo" : {{
             "userName": "{}",
               "firstName": "{}",
              "lastName": "{}",
               "password": "{}",
               "emailAddress": "{}",
               "enabled": true,
               "notificationLanguage": "English",
               "smsServiceProvider": "Global Default"
        }},
        "guestAccessInfo" : {{
             "validDays": "{}",
               "fromDate": "{}",
              "toDate": "{}",
               "location": "San Jose"
        }}
    }}
}}
""".format(portalId,userName,firstName,lastName,password,emailAddress,validDays,fromDate,toDate)

headers = {
    'accept': "application/json",
    'content-type': "application/json",
    'authorization': " ".join(("Basic",encodedAuth)),
    'cache-control': "no-cache",
    }

conn.request("POST", "/ers/config/guestuser/", headers=headers, body=req_body_json)

res = conn.getresponse()
data = res.read()

print("Status: {}".format(res.status))
print("Header:\n{}".format(res.headers))
print("Body:\n{}".format(data.decode("utf-8")))

Execution

python create-guest-user.py 10.20.30.40 sponsor Password1 ce91b520-bb67-11e6-b562-005056873bd0 chris1 Chris Colombus Password1 chris@cisco.com  90 10/07/2016 01/04/2017

Status: 201 (Created)
Header:
Set-Cookie: JSESSIONIDSSO=56D49E483D6BB0A81C8AB8E43E5BA302; Path=/; Secure; HttpOnly
Set-Cookie: APPSESSIONID=6999F8178666C010A2FFC189C362D325; Path=/ers; Secure; HttpOnly
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: https://10.20.30.40:9060/ers/config/guestuser/93d33451-12d3-474e-970d-71c5dccd469e
Date: Thu, 01 Dec 2016 17:34:07 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 0
Server: