Cisco CUCM Extension Mobility Api Python3 – Remote Login

By 7th March 2018Cisco, CUCM, EMAPI

When rolling out extension mobility to large numbers of users, one of your decisions will be whether to log users in prior to go live. Personally I think this helps to enable a smoother transition.
The following example uses the Cisco CUCM extension mobility Api, you can achieve the same using the AXL api in this example.


mac,profile,uid
SEP40A6E8B05B65,UDP_8205702_8861,Mitch.Dawson


__author__ = "Mitch Dawson"
__contact__ = "info@ucdevops.com"
__copyright__ = "Copyright (C) 2017 - 2018 Ucdevops All Rights Reserved"
__license__ = "GPLv3"

"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses.
"""

# Import the requests http library
import requests

# Define our request headers
header = {
    "Content-Type": "application/x-www-form-urlencoded"
}

# Define our individual cucm extension mobility hosts
host = "https://cucm1"

# Define our extension mobility api url
em_url = ":8443/emservice/EMServiceServlet"

# Define our extension mobility proxy user credentials
# The accouunt needs the "Standard EM Authentication Proxy Rights" role applied
proxy_user = "username"
proxy_pass = "password"


# Define our Login class
class Login:

    def __init__(self, url, proxy_user, proxy_password):
        self.url = url
        self.proxy_user = proxy_user
        self.proxy_pass = proxy_password

    def login_msg(self, mac, profile, uid):
        # The function creates our message, required to perform a login request
        # Takes the Phone MAC, Device Profile Name and userid as input
        return """
        <request>
            <appInfo>
                <appID>{0}</appID>
                <appCertificate>{1}</appCertificate>
            </appInfo>
            <login>
                <deviceName>{2}</deviceName>
                <userID>{3}</userID>
                <deviceProfile>{4}</deviceProfile>
                <exclusiveDuration>
                    <indefinite></indefinite>
                </exclusiveDuration>
            </login>
        </request>
        """.format(self.proxy_user, self.proxy_pass, mac, uid, profile)

    def make_request(self, message):
        # The function posts our message to Extension Mobility API
        return requests.post(
            data={"xml": message},
            url=self.url
        )

    def do_login(self, mac, profile, uid):
        # Function which carries out our required steps to perform our login
        print("*" * 50, "\n")
        print(
            "Logging in User '{}' into Handset {} "
            "with Profile {} .....\n"
            .format(uid, mac, profile)
        )
        print(
            self.make_request(
                self.login_msg(mac, profile, uid)
            ).text
        )
        print("*" * 50, "\n")


def main():

    # Open and read our csv input file
    data = open("login.csv", "r").read().split("\n")

    # Instantiate our "Login" class
    login = Login(
        host + em_url, proxy_user, proxy_pass
    )

    # Loop through our login data, skipping our header line
    for line in data[1:]:
        mac, profile, uid = line.split(",")
        login.do_login(mac, profile, uid)


if __name__ == "__main__":
    main()


**************************************************
Logging in User Mitch.Dawson into Handset SEP40A6E8B05B65 with Profile UDP_8205702_8861 .....
<?xml version="1.0"?>
<response>
<success/>
</response>

**************************************************
Mitch

Author Mitch

I am an independent IT Consultant with specialities in Software Development, Enterprise Unified Communication, Network and Security platforms. In addition to my day to day work, I develop bespoke applications and I hope that through ucdevops.com I can build relationships with clients, business partners and fellow engineers by providing solutions to complex problems through the use of programming.

More posts by Mitch

Leave a Reply