Browse By

Add Users in CUCM using Python Script

Add Users in CUCM using Python Script

Hope whosoever reading this post should have basic understanding of python. In this module we will not go deep into python as I am not python expert so some of you may find the script complex but we will try to understand steps being used to achieve the results.

Here we will Add users in CUCM using python script. We will read the user’s details “firstName, lastName, userid” from Users.csv file, will send the SOAP request to CUCM to add the users. print(r.status_code) will print the response code of the request sent [200] in case of successful transaction.

Below is Users.csv file :

import requests ##the requests module allows you to send HTTP requests using Python
import csv ##implements classes to read and write tabular data in CSV format

## The following 3 lines suppresses/ignores the insecure request warnings for unverified https request

import warnings
from requests.packages.urllib3.exceptions import InsecureRequestWarning

warnings.simplefilter(‘ignore’,InsecureRequestWarning)

keys=[‘UserID’, ‘Fname’, ‘Lname’] ## Create list of headers in Users.csv file
r=open(‘Users.csv’) ## Reading the CSV file
read=csv.DictReader(r)
for DevName in read: ## iterate through each row in the file
values=list(map(DevName.get, keys)) ## Building the List of the values for each header for a particular row. For first user it will look like values=[User1, User1, LUser1]
## Below is the SOAP request that we will send to CUCM to add users with given details “firstName, lastName, userid”

soaprequest=”””<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:ns=”http://www.cisco.com/AXL/API/11.5″>
<soapenv:Header/>
<soapenv:Body>
<ns:addUser>
<user>
<firstName>””” +values[1]+ “””</firstName>
<lastName>””” +values[2]+ “””</lastName>
<userid>””” +values[0]+ “””</userid>
</user>
</ns:addUser>
</soapenv:Body>
</soapenv:Envelope>”””
r=requests.post(“https://cucmip/axl/”, verify=False, auth=(‘username’,’password’), data=soaprequest)
print(r.status_code) ##print the response code of the request sent [200] in case of successful transaction.

import requests ##the requests module allows you to send HTTP requests using Python
import csv ##implements classes to read and write tabular data in CSV format
## The following 3 lines suppresses/ignores the insecure request warnings for unverified https requests
import warnings
from requests.packages.urllib3.exceptions import InsecureRequestWarning
warnings.simplefilter('ignore', InsecureRequestWarning)

keys=['UserID', 'Fname', 'Lname'] ## Create list of headers in Users.csv file
r=open('Users.csv') ## Reading the CSV file
read=csv.DictReader(r)
for DevName in read: ## iterate through each row in the file
    values=list(map(DevName.get, keys)) ## Building the List of the values for each header for a particular row. For first user it will look like values=[User1, User1, LUser1]
## Below is the SOAP request that we will send to CUCM to add users with given details "firstName, lastName, userid"

soaprequest="""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">
<soapenv:Header/>
<soapenv:Body>
<ns:addUser>
<user>
<firstName>""" +values[1]+ """</firstName>
<lastName>""" +values[2]+ """</lastName>
<userid>""" +values[0]+ """</userid>
</user>
</ns:addUser>
</soapenv:Body>
</soapenv:Envelope>"""
r=requests.post("https://cucmip/axl/", verify=False, auth=('username','password'), data=soaprequest)
print(r.status_code) ##print the response code of the request sent [200] in case of successful transaction.

 

Hope the document is useful. Please share feedback.

Thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *