Cisco CUCM Extension Mobility Api Python3 – Remote Login

By 7th March 2018Cisco, CUCM, EMAPI
Full Code
 
  1. __author__ = "Mitch Dawson"
  2. __contact__ = "info@ucdevops.com"
  3. __copyright__ = "Copyright (C) 2017 - 2021 Ucdevops All Rights Reserved"
  4. __license__ = "GPLv3"
  5. """
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see http://www.gnu.org/licenses.
  16. """
  17. # Import the requests http library.
  18. import requests
  19. # Define our request headers.
  20. header = {
  21.     "Content-Type": "application/x-www-form-urlencoded"
  22. }
  23. # Define our individual cucm extension mobility hosts.
  24. host = "https://cucm1"
  25. # Define our extension mobility api url.
  26. em_url = ":8443/emservice/EMServiceServlet"
  27. # Define our extension mobility proxy user credentials.
  28. # The account needs the "Standard EM Authentication Proxy Rights" role applied.
  29. proxy_user = "username"
  30. proxy_pass = "password"
  31. # Define our Login class.
  32. class Login:
  33.     def __init__(self, url, proxy_user, proxy_password):
  34.         self.url = url
  35.         self.proxy_user = proxy_user
  36.         self.proxy_pass = proxy_password
  37.     def login_msg(self, mac, profile, uid):
  38.         # The function creates our message, required to perform a login request.
  39.         # Takes the Phone MAC, Device Profile Name and userid as input.
  40.         return """
  41.         <request>
  42.             <appInfo>
  43.                 <appID>{0}</appID>
  44.                 <appCertificate>{1}</appCertificate>
  45.             </appInfo>
  46.             <login>
  47.                 <deviceName>{2}</deviceName>
  48.                 <userID>{3}</userID>
  49.                 <deviceProfile>{4}</deviceProfile>
  50.                 <exclusiveDuration>
  51.                     <indefinite></indefinite>
  52.                 </exclusiveDuration>
  53.             </login>
  54.         </request>
  55.         """.format(self.proxy_user, self.proxy_pass, mac, uid, profile)
  56.     def make_request(self, message):
  57.         # The function posts our message to Extension Mobility API
  58.         return requests.post(
  59.             data={"xml": message},
  60.             url=self.url
  61.         )
  62.     def do_login(self, mac, profile, uid):
  63.         # Function which carries out our required steps to perform our login.
  64.         print("*" * 50, "\n")
  65.         print(
  66.             "Logging in User '{}' into Handset {} "
  67.             "with Profile {} .....\n"
  68.             .format(uid, mac, profile)
  69.         )
  70.         print(
  71.             self.make_request(
  72.                 self.login_msg(mac, profile, uid)
  73.             ).text
  74.         )
  75.         print("*" * 50, "\n")
  76. def main():
  77.     # Open and read our csv input file.
  78.     data = open("login.csv", "r").read().split("\n")
  79.     # Instantiate our "Login" class.
  80.     login = Login(
  81.         host + em_url, proxy_user, proxy_pass
  82.     )
  83.     # Loop through our login data skipping our header line.
  84.     for line in data[1:]:
  85.         mac, profile, uid = line.split(",")
  86.         login.do_login(mac, profile, uid)
  87. if __name__ == "__main__":
  88.     main()
  89.  
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

"