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.

Synology: Remove orphaned AWR files when JPG is deleted in Photo Studio 6

Background

Hardware: Synology DS716+
Software: Synology Photo Station 6
Data Files: .jpg & .arw (raw)

The problem

When using a Synology NAS to manage your photos via the Photo Station 6 application when I delete the JPG the RAW (ARW) remains behind.

The solution

Search the photo directory for orphan .arw files (ones without a matching .jpg), then remove it. While we are at it, lets record what we delete to a file.

Deploy an Ubuntu docker image and mount the photos directory


Use the code


#!/usr/bin/python
import os
rootdir = '/mnt/photo/Dump/2016/2016-02_Muppo-playing'
files = os.listdir(rootdir)
for file in files:
if file.endswith('.ARW'):
filename, file_ext = os.path.splitext(rootdir + '/' + file)
if not os.path.isfile(filename + '.JPG'):
os.remove(rootdir + '/' + file)
print('REMOVED:' + rootdir + '/' + file)
with open("clean-up.log", "a") as logfile:
logfile.write("\n")
logfile.write('REMOVED:' + rootdir + '/' + file)

Using PowerShell to produce a list of databases from a list of server\instances

Need to produce a report showing all the databases in your environment? Why not include the name, size and owner while we are at it and export it to a csv file. Here you go!

Note: The SQLPS module is installed on a machine with Microsoft SQL Server Management Studio. I have tested this with MSSMS 2014.

The PowerShell Script

import-module "C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\SQLPS" -DisableNameChecking
$rootdir = "C:\Users\VirtualRick\SQL Server Audit\"
$instances = import-csv $rootdir\server-instance.csv
ForEach($row in $instances)
{
$sqlPath = "SQLSERVER:\SQL\$($row.server)\$($row.instance)\Databases\"
dir $sqlPath | select Name, Size, Owner | export-csv $rootdir\export.csv -Append
}

 

server-instance.csv file example:

Server,Instance

MyServer,default

Upload local file to Azure File Share via Power Shell

Want to upload a log file to your cloud storage? Here is a quick and easy way to do it via Power Shell.


$AccountName = "YOURACCOUNTNAMEHERE"
$AccountKey = "YOURACCESSKEYHERE"
$Context01 = New-AzureStorageContext -StorageAccountKey $AccountKey -StorageAccountName $AccountName
Set-AzureStorageFileContent -ShareName backups -Source "C:\file.log" -Path "DestinationFile.Name" -Context $Context01

PowerShell script to compare the contents of two folders

Need to find out if two folders have the same files in them? Here is a quick PowerShell script to make it happen!

PS C:\Windows\system32> $server1 = get-childitem -Recurse -path E:\folder1
PS C:\Windows\system32> $server2 = get-childitem -Recurse -path \\server2\folder1
PS C:\Windows\system32> Compare-Object -ReferenceObject $server1 -DifferenceObject $server2

InputObject SideIndicator
———– ————-
Sample.file.txt <=

 

References:

https://blogs.technet.com/b/heyscriptingguy/archive/2011/10/08/easily-compare-two-folders-by-using-powershell.aspx

Obtain users DN via PowerShell

Note to self… ┬áThis is how you can get the correct DN, and other information, for a user in AD.

From PowerShell

import-module activedirectory

get-aduser -filter {EmailAddress -like “rick@virtualrick.*”}

or

get-aduser -filter {samAccountName -like “rick*”}