Make and AXL Query Against CUCM with Python

I recently had a need to interact with Cisco Unified Communications Manager (CUCM) in an automated way. Some quick searched returned some code that worked a little bit, but not all the way. Here is the code that I ended up with that works.


#!/usr/bin/python
#The purpose of this script is to execute a basic query against CUCM via AXL
from suds.client import Client
from suds.sax.element import Element
import base64
import ssl
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
cmserver = 'IP-ADDRESS'
cmport = '8443'
wsdl = 'file:///var/www/html/AXLAPI.wsdl'
location = 'https://' + cmserver + ':' + cmport + '/axl/'
username = 'MyAXLUser'
password = 'MyPassword'
def getUser(userName):
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
authenticationHeader = {
"SOAPAction" : "ActionName",
"Authorization" : "Basic %s" % base64string
}
client = Client(wsdl, location=location, username=username, password=password, headers=authenticationHeader)
userName = str(userName)[:-1].lower()
agentName = Element('userid').setText(userName)
try:
getUser = client.service.getUser(agentName)
DEVICEID = getUser[0][0][11][0][0]
return DEVICEID
except:
return "no device associated"
print getUser('Rick.Breidenstein')

GitHub Link: Here it is

 

 

Snippets of problems and errors along the way

Hopefully providing the errors that I came across will help others with the same issue find a solution faster.

Creating the unverified ssl context resolved the issue below

Traceback (most recent call last):
File "test.py", line 15, in
result = client.service.getOSVersion()
File "build/bdist.linux-x86_64/egg/suds/client.py", line 535, in __call__
File "build/bdist.linux-x86_64/egg/suds/client.py", line 595, in invoke
File "build/bdist.linux-x86_64/egg/suds/client.py", line 620, in send
File "build/bdist.linux-x86_64/egg/suds/transport/http.py", line 85, in send
File "build/bdist.linux-x86_64/egg/suds/transport/http.py", line 107, in __open
File "/usr/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1241, in https_open
context=self._context)
File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError:

adding the authentication header resolved the issue below

Traceback (most recent call last):
File "test.py", line 16, in
result = client.service.getOSVersion()
File "build/bdist.linux-x86_64/egg/suds/client.py", line 535, in __call__
File "build/bdist.linux-x86_64/egg/suds/client.py", line 595, in invoke
File "build/bdist.linux-x86_64/egg/suds/client.py", line 630, in send
File "build/bdist.linux-x86_64/egg/suds/client.py", line 687, in failed
Exception: (401, u'Unauthorized')

Introducing the Element to pass through the suds client resolved the issue below

No handlers could be found for logger "suds.client"
Traceback (most recent call last):
File "test.py", line 29, in
result = client.service.getPhone(name = 'SEPD0C282D1ECE0')
File "build/bdist.linux-x86_64/egg/suds/client.py", line 535, in __call__
File "build/bdist.linux-x86_64/egg/suds/client.py", line 595, in invoke
File "build/bdist.linux-x86_64/egg/suds/client.py", line 630, in send
File "build/bdist.linux-x86_64/egg/suds/client.py", line 681, in failed
File "build/bdist.linux-x86_64/egg/suds/bindings/binding.py", line 235, in get_fault
suds.WebFault: Server raised fault: 'No uuid or name element found'

Error below became present when passing the user name through as a variable. Removing the last character from the string resolved this.

No handlers could be found for logger "suds.client"
Traceback (most recent call last):
File "test.py", line 29, in
result = client.service.getUser(user = 'Tony.Hasting')
File "build/bdist.linux-x86_64/egg/suds/client.py", line 535, in __call__
File "build/bdist.linux-x86_64/egg/suds/client.py", line 595, in invoke
File "build/bdist.linux-x86_64/egg/suds/client.py", line 630, in send
File "build/bdist.linux-x86_64/egg/suds/client.py", line 681, in failed
File "build/bdist.linux-x86_64/egg/suds/bindings/binding.py", line 235, in get_fault
suds.WebFault: Server raised fault: 'Item not valid: The specified User was not found'

Error below is when you do not create an element for searchCriteria and returnedTags. reference the listUser function.

No handlers could be found for logger "suds.client"
Traceback (most recent call last):
File "./cucm-axl.py", line 46, in
listUser(user[:-1])
File "./cucm-axl.py", line 42, in listUser
result = client.service.listUser()
File "build/bdist.linux-x86_64/egg/suds/client.py", line 535, in __call__
File "build/bdist.linux-x86_64/egg/suds/client.py", line 595, in invoke
File "build/bdist.linux-x86_64/egg/suds/client.py", line 630, in send
File "build/bdist.linux-x86_64/egg/suds/client.py", line 681, in failed
File "build/bdist.linux-x86_64/egg/suds/bindings/binding.py", line 235, in get_fault
suds.WebFault: Server raised fault: 'No Search Criteria Defined'

References:

This is the result from a collection of many different sources. Sorry I do no have all the links to site.

Leave a Reply