Python Script to get the device pool of Cisco IP Phones from CUCM

Python Script to get the device pool of phones from CUCM:

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 read phone’s MAC address from Phone.csv file, will send SOAP request to CUCM.  CUCM returns data for that phone and we will pull only the device pool from the response returned from CUCM and will store the result in DevicePool.csv file.

We have two columns in Phone.csv file which are Phone and Model and two columns in DevicePool.csv which are Phone and Devicepool.

Below is Phone.csv file :

Explanation of the steps used in script:

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
import xml.etree.ElementTree as ET ##used to parse and create XML data

## 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)

## The following lines of code opens “Devicepool.csv” file in write mode and defines headers/coulmns “Phone and DevicePool”
f=open(‘Devicepool.csv’,’w’)
fieldnames=[‘Phone’, ‘DevicePool’]
w=csv.DictWriter(f,fieldnames=fieldnames, lineterminator=’\n’)
w.writeheader()

r=open(‘Phone.csv’) ## Phone.csv file has list of phones whose device pool we need to find. This file has 2 coulmns name Phone and Model. Phone contains MAC addresses of phones and Model lists the model of the phones, so we will be reading phone’s MAC address from this file
read=csv.DictReader(r)
for DevName in read: ## Here we will iterate through each row in the file
try:
phone=DevName.get(“Phone”) ## Get only the MAC address of the device which is listed under coulmn Phone in file Phone.csv and store it in variable phone.
## Below is the soap request that we will send to CUCM using post method and cucm will return the XML response of phone configuration
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:getPhone>
<name>””” + phone + “””</name>
</ns:getPhone>
</soapenv:Body>
</soapenv:Envelope>”””
r=requests.post(“https://cucmip/axl/”, verify=False, auth=(‘username’,’password’), data=soaprequest)
XMLDoc=ET.fromstring(r.content) ## Here we will parse the XML data from string which is the content returned in HTTP response

## XML data contains so many tags like name of the phone, description, product, model, etc. We will be reading specific element (device pool) from XML document with “.//devicePoolName”. devicePoolName is one of the tags returned in response.
devicepool=XMLDoc.find(“.//devicePoolName”).text
print(phone+ “,” +devicepool)
f=open(‘Devicepool.csv’,’w’, newline=”)
w.writerow({‘Phone’:phone, ‘DevicePool’:devicepool}) ## Here we will write the phone and device pool information in Devicepool.csv file
f.close()
except: ## We will use try and except for exception handling in case phone that we read from Phone.csv file is not provisioned in CUCM
print(“Phone Not Found, please add the phone if required”)

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
import xml.etree.ElementTree as ET ##used to parse and create XML data

## 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)

## The following lines of code opens "Devicepool.csv" file in write mode and defines headers/coulmns "Phone and DevicePool"
f=open('Devicepool.csv','w')
fieldnames=['Phone', 'DevicePool']
w=csv.DictWriter(f,fieldnames=fieldnames, lineterminator='\n')
w.writeheader()

r=open('Phone.csv') ## Phone.csv file has list of phones whose device pool we need to find. This file has 2 coulmns name Phone and Model. Phone contains MAC addresses of phones and Model lists the model of the phones, so we will be reading phone's MAC address from this file
read=csv.DictReader(r)
for DevName in read: ## Here we will iterate through each row in the file
    try:
        phone=DevName.get("Phone") ## Get only the MAC address of the device which is listed under coulmn Phone in file Phone.csv and store it in variable phone.
        ## Below is the soap request that we will send to CUCM using post method and cucm will return the XML response of phone configuration
        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:getPhone>
        <name>""" + phone + """</name>
        </ns:getPhone>
        </soapenv:Body>
        </soapenv:Envelope>"""
        r=requests.post("https://cucmip/axl/", verify=False, auth=('username','password'), data=soaprequest)
        XMLDoc=ET.fromstring(r.content) ## Here we will parse the XML data from string which is the content returned in HTTP response

        ## XML data contains so many tags like name of the phone, description, product, model, etc. We will be reading specific element (device pool) from XML document with ".//devicePoolName". devicePoolName is one of the tags returned in response.
        devicepool=XMLDoc.find(".//devicePoolName").text
        print(phone+ "," +devicepool)
        f=open('Devicepool.csv','w', newline='')
        w.writerow({'Phone':phone, 'DevicePool':devicepool}) ## Here we will write the phone and device pool information in Devicepool.csv file
        f.close()
    except: ## We will use try and except for exception handling in case phone that we read from Phone.csv file is not provisioned in CUCM
        print("Phone Not Found, please add the phone if required")

 

The resultant Devicepool.csv file:

Hope the document is  useful to understand the how to form basic requests and action on the data returned. Please share feedback.

Thank you

You may also like...

Leave a Reply

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