cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements

This community is for technical, feature, configuration and deployment questions.
For production deployment issues, please contact the TAC! We will not comment or assist with your TAC case in these forums.
Please see How to Ask the Community for Help for other best practices.

1253
Views
0
Helpful
4
Replies

Cisco ISE and python Integration

I am  new  to cisco ISE and writing a python scripts to create internal user on ISE . I am having problem of executing it  on  cisco IISE .Can anyone help me out 

1 ACCEPTED SOLUTION

Accepted Solutions
hslai
Cisco Employee

python ISEInternalUserTest.py 192.198.1.1 ers-admin admin20! robert Robert Mensah Password1 rmensah@gha.com Employee 12/26/2020

"12/26/2020" should be sys.argv[10] since sys.arg[1] is "192.198.1.1".

View solution in original post

4 REPLIES 4
hslai
Cisco Employee

Hi hslai

I am try to  create internal  users with my Python script. This is my code I  created 

#!/usr/bin/python
import http.client
import base64
import ssl
import sys
import os
argv = sys.argv[1]
print ("The name of the script: ", sys.argv[0])
print ("The argument are: ", argv)
#parameters

Id = sys.argv[4] # "B100"
userName = sys.argv[5] # "Robert"
firstName = sys.argv[6] # "Robert"
lastName = sys.argv[7] # "Mensah"
password = sys.argv[8] # "Password1"
emailAddress = sys.argv[9] # "rmensah@cisco.com
expiryDate = sys.argv[11] # “12/26/2020”
identityGroups = sys.argv[10] # “identityGroups”


# host and authentication credentials
host = sys.argv[1] # "192.198”.1.1
user = sys.argv[2] # “ers-admin”
password = sys.argv[3] # “admin20!”

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

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

req_body_json = """{{

\"InternalUser"\ : {

"id" : "\{}\",

"userName" : "\{}\",

"description" : "\{}\",

"enabled" : true,

"email" : "\{}\",

"password" : "\{}\",

"firstName" : "\{}\",

"lastName" : "\{}\",

"changePassword" : true,

"identityGroups" : "\{}\",

"expiryDateEnabled" : false,

"expiryDate" : "\{}\",

"enablePassword" : "enablePassword",

"customAttributes" : {

"key1" : "value1",

"key2" : "value3"

},

"passwordIDStore" : "Internal Users"

}

}}
}}""".format(Id,UserNname,firstName,lastName,emailAddress,password,identityGroups,expiryDate)
headers = {
'accept': "application/json",
'content-type': "application/json",
'authorization': " ".join(("Basic",encodedAuth)),
'cache-control': "no-cache",
}
conn.request("POST", "/ers/config/internaluser/", headers=headers, body=req_body_json)

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

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

 

I got this error while  executing the above code

C:\Users\ghibuser\Desktop>python ISEInternalUserTest.py 192.198.1.1 ers-admin admin20! robert Robert Mensah Password1 rmensah@gha.com Employee 12/26/2020
The name of the script: ISEInternalUserTest.py
The argument are: 192.198.1.1
Traceback (most recent call last):
Traceback (most recent call last):
File "C:\Users\ghibuser\Desktop\ISEInternalUserTest.py", line 38, in <module>
req_body_json = """{{
IndexError: Replacement index 7 out of range for positional args tuple

 

Any support will be welcomed 

hslai
Cisco Employee

python ISEInternalUserTest.py 192.198.1.1 ers-admin admin20! robert Robert Mensah Password1 rmensah@gha.com Employee 12/26/2020

"12/26/2020" should be sys.argv[10] since sys.arg[1] is "192.198.1.1".

View solution in original post

Hi @hslai 

This is my code 

#!/usr/bin/python
import http.client
import base64
import ssl
import sys
import os
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

 

argv = sys.argv[1]
print ("The name of the script: ", sys.argv[0])
print ("The argument are: ", argv)
#parameters


userName = sys.argv[5] # "robert"
firstName = sys.argv[6] # "Robert"
lastName = sys.argv[7] # " Mensah"
password = sys.argv[8] # "Password1"
emailAddress = sys.argv[9] # "rmensah@gh.com
expiryDate = sys.argv[10] # “12/26/2022”
identityGroups = sys.argv[4] # “Employee”


# host and authentication credentials
host = sys.argv[1] # "192.198”.1.1
user = sys.argv[2] # “ers-admin”
password = sys.argv[3] # “admin20!”

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

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

req_body_json = """{{

\"InternalUser\" : {{


\"userName\" : "\{}\",


\"description"\ : "\{}\",

"enabled" : true,

\"email\" : "\{}\",

\"password\" : "\{}\",

\"firstName\" : "\{}\",

\"lastName\" : "\{}\",

\"changePassword\" : true,

\"identityGroups\" : "\{}\",

"expiryDateEnabled" : false,

"expiryDate" : "\{}\",

"enablePassword" : "enablePassword",

"customAttributes" : {

"key1" : "value1",

"key2" : "value3"

},

"passwordIDStore" : "Internal Users"

}

}}
}}""".format(userName,firstName,lastName,emailAddress,password,identityGroups,expiryDate)
headers = {
'accept': "application/json",
'content-type': "application/json",
'authorization': " ".join(("Basic",encodedAuth)),
'cache-control': "no-cache",
}
conn.request("POST", "/ers/config/internaluser/", headers=headers, body=req_body_json)

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

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

print(req_body_json)

 

 

 

can you help me with this error

IndexError: Replacement index 7 out of range for positional args tuple

Create
Recognize Your Peers
Polls
Which of these topics should we host an event in the Community?

Top Choice: ISE- Guest and Posture Troubleshooting (34%)

Content for Community-Ad

ISE Webinars



Did you miss a previous ISE webinar?

CiscoISE YouTube Channel