Use Python to Lookup a device from Cisco Unified Communications Manager
«« Connecting to CUCM from Python
In order to make things a bit easier, I set my variables in a file called config.py
and read that so I only have to change things in one place for all the scripts that I use to connect to CUCM.
The password is saved in the file, it is base64 encoded. While this is not the ideal way to protect a password, it does keep some casually prying eyes from seeing the password.
WARNING: Do NOT use something like this in production. This is not secure at all
To create the encoded string, use the following
import base64
base64.b64encode("password".encode('ascii'))
You will get an output similar to b'cGFzc3dvcmQ='
, this is the base64 encrypted string. You need to strip off the leading b'
and just save the string as 'cGFzc3dvcmQ='
.
CONFIG.py
# config.py:
import base64
from pathlib import Path
WSDL_URL = 'file://' + Path("resources/AXLAPI.wsdl").absolute().as_posix()
CUCM_URL = "https://127.0.0.1:8443/axl/"
CUCM_USERNAME = "axluser"
CUCM_ENCPASS = 'cGFzc3dvcmQ='
CUCM_PASSWD = base64.b64decode(CUCM_ENCPASS).decode("utf-8")
Now every script that you use to connect to CUCM can get the variables from this file.
Lookup Phone from CUCM
# LookupPhoneFromCUCM.py
import sys, getopt
from requests import Session
from requests.auth import HTTPBasicAuth
from zeep import Client
from zeep.transports import Transport
from zeep.cache import SqliteCache
import urllib3
#Setup the argument variables
devicename = input("Enter the Name of the Device to Lookup: ")
#Get the variables from config.py
from config import *
#Connect to CUCM
# disable Insecure Request Warning due to Verify
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session = Session()
session.verify = False
session.auth = HTTPBasicAuth(CUCM_USERNAME, CUCM_PASSWD)
transport = Transport(session=session, timeout=10, cache=SqliteCache())
client = Client(WSDL_URL, transport=transport)
service = client.create_service("{http://www.cisco.com/AXLAPIService/}AXLAPIBinding", CUCM_URL)
#Get the phone and output the response
resp = service.getPhone(name=devicename)
try:
resp
except NameError:
print("device not found")
else:
print(resp)