CUCM AXL API – Bulk Logout of Extension Mobility Profile Using Python

In our last blog, we covered how to set up the environment for running AXL API commands for CUCM and implemented a Python program for removing directory numbers. Now, we’ll create another Python program that enables bulk logout of extension mobility profiles from phone devices. You’ll simply need to provide a list of MAC addresses in an Excel sheet.

Going for Bulk logout of extension mobility

In the SOAP UI application, navigate to the left panel and expand AXLAPIBinding. Next, locate and expand doDeviceLogout to open the request window.

CUCM AXL API-Bulk Logout of Extension Mobility Profiles Using Python

Extract the MAC address to uniquely identify the device.  Remove the UUID portion and replace the question mark with your actual MAC address.

CUCM AXL API-Bulk Logout of Extension Mobility Profiles Using Python

With the XML request prepared, you can test it using Postman before converting it to Python code.

CUCM AXL API-Bulk Logout of Extension Mobility Profiles Using Python

After running the command, you’ll see a successful response indicated by a 200 OK status code and a message, similar to the one shown in the screenshot above.

Python Code

import requests
import base64
import openpyxl

# Define the AXL API URL
url = "https://hq-cucm-pub.ciscouc.com/axl/"

# Define username and password
username = "axlapiuser"
password = "axlapiuser"

# Define request headers, including Basic Authentication
headers = {
    'Content-Type': 'text/xml',
    'Authorization': f'Basic {base64.b64encode(f"{username}:{password}".encode()).decode()}'
}

def device_logout(device_name):
    # Define the SOAP request payload
    payload = f"""
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/12.5">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:doDeviceLogout sequence="">
             <deviceName>{device_name}</deviceName>
          </ns:doDeviceLogout>
       </soapenv:Body>
    </soapenv:Envelope>
    """

    try:
        # Send a POST request to the AXL API
        response = requests.post(url, headers=headers, data=payload, verify=False)

        # Check if the request was successful (status code 200)
        if response.status_code == 200:
            print(f"Request for device '{device_name}' was successful. Response:")
            print(response.text)
        else:
            print(f"Request for device '{device_name}' failed with status code: {response.status_code}")
            print(response.text)
    except requests.exceptions.RequestException as e:
        print(f"An error occurred while sending the request: {e}")

if __name__ == "__main__":
    # Load the Excel file
    workbook = openpyxl.load_workbook('data.xlsx')
    sheet = workbook.active

    # Iterate through rows in the Excel file to get device names
    for row in sheet.iter_rows(values_only=True):
        device_name = row[0]  
        
        # Skip empty rows
        if device_name is None:
            continue
        
        device_logout(device_name)

    workbook.close()  # Close the Excel file after usage

Github Link: https://github.com/anashira/CUCM-AXL-Extension-Mobility-Logout

Update the username, password, and URL fields with your own credentials.

Below is the screenshot of my sample excel sheet where I supplied the MAC address of the phones

CUCM AXL API SOAP UI and Python

I hope you found this article useful. Your feedback and comments are highly appreciated. Feel free to leave a comment below or reach out to me on LinkedIn.

One Comment

Add a Comment

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