Skip to content

Instantly share code, notes, and snippets.

@hf0002
Created March 24, 2020 04:36
Show Gist options
  • Save hf0002/3f0b5a9e88e8f7ea90cdccb93faabc7e to your computer and use it in GitHub Desktop.
Save hf0002/3f0b5a9e88e8f7ea90cdccb93faabc7e to your computer and use it in GitHub Desktop.
Magic Jabber Button
{
"ldap_hostname": "ds.uah.edu",
"ldap_bind_dn": "CN=The CN of the service account,OU=Managed Services,OU=OITAccounts,DC=DS,DC=UAH,DC=edu",
"ldap_base_dn": "DC=DS,DC=UAH,DC=edu",
"ldap_bind_password": "That service account password",
"wsdl":"file:///home/uahusers/hf0002/Projects/axlrows/axlsqltoolkit/schema/11.5/AXLAPI.wsdl",
"location":"https://vbhucmpub.voip.uah.edu:8443/axl/",
"username":"whoever",
"password":"whatever"
}
import ldap3
import ssl
import json
import sys
import suds
from axlrows import CiscoUCM
from datetime import datetime
from flask import Flask, request, render_template, Response
debug_ip = 'THIS_SERVERS_IP'
debug_port = 8081
app = Flask(__name__)
config = None
with open("config.json", 'r') as stream:
config = json.load(stream)
def get_by_dn(dn, attributes=['sAMAccountName', 'mail', 'telephoneNumber', 'ipPhone']):
ldap_c.search(config['ldap_base_dn'], '(distinguishedName=' + dn + ')', attributes=attributes)
return ldap_c.entries[0]
def mail_to_dn(mail):
ldap_c.search(config['ldap_base_dn'], '(userPrincipalName=' + mail + ')')
return ldap_c.entries[0].entry_get_dn()
#set up UCM
ucm = CiscoUCM()
#set up ldap
ldap_tls = ldap3.Tls(
validate=ssl.CERT_REQUIRED,
version=ssl.PROTOCOL_TLSv1,
ca_certs_file='/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem'
)
ldap_server = ldap3.Server(
config['ldap_hostname'],
use_ssl=True,
tls=ldap_tls
)
@app.route("/jabber-device", methods=['POST'])
def do_the_thing():
ad = ldap3.Connection(
ldap_server,
config['ldap_bind_dn'],
config['ldap_bind_password'],
auto_bind=True,
pool_lifetime=300
)
assert ad.result['result'] == 0, "AD connection was not successful"
upn = request.form.get("upn")
smartphone_type = request.form.get("smartphoneType")
print("upn:", upn, "smartphoneType:", smartphone_type)
if smartphone_type == "Windows" or smartphone_type == "macOS":
smartphone_type = "PC"
if smartphone_type != "iPhone" and smartphone_type != "Android" and smartphone_type != "PC":
return "I don't know how to make a Jabber device for that smartphoneType", 501
ad.search(config['ldap_base_dn'], '(userPrincipalName=' + upn + ')', attributes=['ipPhone', 'displayName'])
if not 'ip phone' in ad.entries[0]:
return "Not creating for " + upn + " because they don't have an ipPhone", 403
user_display_name = str(ad.entries[0]['display name'])
description = user_display_name + " (" + smartphone_type + ") - Magic Jabber Button"
sip_profile_name = "Standard SIP Profile"
if smartphone_type == "PC":
product = "Cisco Unified Client Services Framework"
phone_template_name ="Standard Client Services Framework"
security_profile_name = "Cisco Unified Client Services Framework - Standard SIP Non-Secure Profile"
phone_name_prefix = "CSF"
else:
product = "Cisco Dual Mode for " + smartphone_type
phone_template_name = "Standard Dual Mode for " + smartphone_type
security_profile_name = "Cisco Dual Mode for " + smartphone_type + " - Standard SIP Non-Secure Profile"
sip_profile_name = sip_profile_name + " for Mobile Device",
phone_name_prefix = "TCT" if smartphone_type == "iPhone" else "BOT"
phone_name = phone_name_prefix + upn.split("@", 1)[0].upper()
dn = str(ad.entries[0]['ip phone'])
print("ipPhone:", dn)
try:
ucm.add_phone(
name=phone_name,
description=description,
product=product,
deviceClass="Phone",
protocol="SIP",
devicePoolName="Default",
phoneTemplateName=phone_template_name,
callingSearchSpaceName="Default-Outbound-CSS",
networkHoldMohAudioSourceId="1",
userHoldMohAudioSourceId="1",
ownerUserName=upn,
securityProfileName=security_profile_name,
sipProfileName=sip_profile_name,
lines=[ { "line": {
"index": "1",
"label": dn,
"display": user_display_name,
"displayAscii": user_display_name,
"dirn": {
"pattern":dn,
"routePartitionName": "YOUR_USERS_ROUTE_PARTITION"
},
"associatedEndusers": [
{
"enduser": {
"userId": upn
}
}
]
} } ]
)
except suds.WebFault as e:
print("===", upn, smartphone_type, "===")
print(e)
if "duplicate value" in str(e):
return "already exists", 204
else:
return str(e), 500
return "created!", 201
if __name__ == '__main__':
app.run(host=debug_ip, port=debug_port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment