Phone Services
The phone can make HTTP requests to a remote web-server to display text, menus and images as well as check whether a CGI Execute requests will be allowed. URLs assigned to fixed-keys (eg: services, directories) and line keys are defined in SEPMAC.cnf.xml. An archive containing a sample WSGI application can be downloaded from the URL below.file_download services-2.4.tar.gz (12K) event 23/05/2022 security SHA256:2fe7aeb520a6eff9da772ce94fe1858cafc7cbdeebf3d4ce403e269d516cb548.
See open_in_browser Cisco Unified IP Phone Services Application Development Notes for a list of XML objects the each phone model supports.
authenticationURL link
If defined, the phone will make requests to this URL containing the username and password that was included in theAuthorization
header in the request to http://x.x.x.x/CGI/Execute. See CGI Execute for more information.The script handling the request must respond exactly with
AUTHORIZED
(with no newline character at end) otherwise the request will be denied. Example below provides this on /authentication
.#!/usr/bin/python3
import os.path
import re
from flask import Blueprint, Response, request, g
import config
blueprint = Blueprint('authentication', __name__)
@blueprint.route('/authentication')
def cgi_authentication():
username = request.args.get('UserID', '')
password = request.args.get('Password', '')
if username != config.cgi_username or password != config.cgi_password:
return Response('UNAUTHORIZED', mimetype = 'text/plain'), 200
return Response('AUTHORIZED', mimetype = 'text/plain'), 200
@blueprint.before_request
def before_request():
device_name = request.args.get('devicename', '')
# check that the device name is valid
if not re.search(r'(?x) ^ SEP [0-9A-F]{12} $', device_name) or \
not os.path.exists(f'{config.tftpboot_dir}/{device_name}.cnf.xml'):
return Response('ERROR'), 403
g.device_name = device_name
return None
@blueprint.errorhandler(Exception)
def error_handler(error):
return Response('ERROR', mimetype = 'text/plain'), 500
directoryURL link
The URL accessed when the directories button is pressed. Example below provides this on/directory
and /directory/79xx
(7900 series). Requires that the Asterisk manager web-interface is enabled.#!/usr/bin/python3
import os.path
import re
from math import ceil
from urllib.parse import quote_plus
from html import escape
import requests
from lxml import etree
from flask import Blueprint, Response, request, g
import config
blueprint = Blueprint('directory', __name__)
@blueprint.route('/directory')
def directory_index():
xml = '<?xml version="1.0" encoding="UTF-8"?>' \
'<CiscoIPPhoneMenu>' \
'<Title>Local Directory</Title>'
for index in ('1', '2ABC', '3DEF', '4GHI', '5JKL', '6MNO', '7PRQS', '8TUV', '9WXYZ', '0'):
xml += '<MenuItem>' \
'<Name>' + escape(index) + '</Name>' \
'<URL>' + request.url_root + 'directory/entries?name=' + quote_plus(g.device_name) + '&index=' + quote_plus(index) + '</URL>' \
'</MenuItem>'
if g.is_79xx:
xml += '<Prompt>Your current options</Prompt>'
xml += '<SoftKeyItem>' \
'<Name>Exit</Name>' \
'<Position>' + ('3' if g.is_79xx else '1') + '</Position>' \
'<URL>Init:Directories</URL>' \
'</SoftKeyItem>' \
'<SoftKeyItem>' \
'<Name>' + ('Select' if g.is_79xx else 'View') + '</Name>' \
'<Position>' + ('1' if g.is_79xx else '2') + '</Position>' \
'<URL>SoftKey:Select</URL>' \
'</SoftKeyItem>' \
'<SoftKeyItem>' \
'<Name>Help</Name>' \
'<Position>' + ('2' if g.is_79xx else '3') + '</Position>' \
'<URL>' + request.url_root + 'directory/help?name=' + quote_plus(g.device_name) + '</URL>' \
'</SoftKeyItem>' \
'</CiscoIPPhoneMenu>'
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.route('/directory/entries')
def directory_entries():
index = request.args.get('index', '')
if not len(index):
return directory_index()
session = requests.Session()
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'Login',
'Username': config.manager_username,
'Secret': config.manager_secret})
response.raise_for_status()
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'VoicemailUsersList'})
response.raise_for_status()
document = etree.fromstring(response.content)
entries = []
for element in document.findall('response/generic[@event="VoicemailUserEntry"]'):
extension = element.get('voicemailbox')
name = element.get('fullname', '')
if name[0].upper() not in index:
continue
entries.append((extension, name))
entries.sort(key = lambda entry: entry[1])
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'Logoff'})
response.raise_for_status()
# 10 entries per page
pages = ceil(len(entries) / 10)
try:
page = int(request.args.get('page', '1'))
if page > pages:
raise ValueError
except ValueError:
page = 1
xml = '<?xml version="1.0" encoding="UTF-8"?>' \
'<CiscoIPPhoneDirectory>' \
'<Title>' + escape(index) + (' ' + str(page) + '/' + str(pages) if pages > 1 else '') + '</Title>'
for extension, name in entries[(page - 1) * 10:page * 10]:
xml += '<DirectoryEntry>' \
'<Name>' + escape(name) + '</Name>' \
'<Telephone>' + quote_plus(extension) + '</Telephone>' \
'</DirectoryEntry>'
if g.is_79xx:
xml += '<Prompt>Select entry</Prompt>'
xml += '<SoftKeyItem>' \
'<Name>Exit</Name>' \
'<Position>' + ('3' if g.is_79xx else '1') + '</Position>' \
'<URL>' + request.url_root + 'directory?name=' + quote_plus(g.device_name) + '</URL>' \
'</SoftKeyItem>' \
'<SoftKeyItem>' \
'<Name>' + ('Dial' if g.is_79xx else 'Call') + '</Name>' \
'<Position>' + ('1' if g.is_79xx else '2') + '</Position>' \
'<URL>SoftKey:Select</URL>' \
'</SoftKeyItem>'
if page < pages:
xml += '<SoftKeyItem>' \
'<Name>Next</Name>' \
'<URL>' + request.url_root + 'directory/entries?name=' + quote_plus(g.device_name) + '&index=' + quote_plus(index) + '&page=' + str(page + 1) + '</URL>' \
'<Position>' + ('2' if g.is_79xx else '3') + '</Position>' \
'</SoftKeyItem>'
if page > 1:
xml += '<SoftKeyItem>' \
'<Name>Previous</Name>' \
'<URL>' + request.url_root + 'directory/entries?name=' + quote_plus(g.device_name) + '&index=' + quote_plus(index) + '&page=' + str(page - 1) + '</URL>' \
'<Position>' + ('4' if g.is_79xx else '4') + '</Position>' \
'</SoftKeyItem>'
xml += '</CiscoIPPhoneDirectory>'
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.route('/directory/help')
def directory_help():
xml = '<?xml version="1.0" encoding="UTF-8"?>' \
'<CiscoIPPhoneText>' \
'<Title>How To Use</Title>' \
'<Text>Use the keypad or navigation key to select the first letter of the person\'s name.</Text>'
if g.is_79xx:
xml += '<Prompt>Your current options</Prompt>'
xml += '<SoftKeyItem>' \
'<Name>Back</Name>' \
'<URL>SoftKey:Exit</URL>' \
'<Position>' + ('3' if g.is_79xx else '1') + '</Position>' \
'</SoftKeyItem>' \
'</CiscoIPPhoneText>'
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.route('/directory/79xx')
def directory_menuitem():
# 79xx series need a menu item before the index
xml = '<?xml version="1.0" encoding="UTF-8"?>' \
'<CiscoIPPhoneMenu>' \
'<MenuItem>' \
'<Name>Local Directory</Name>' \
'<URL>' + request.url_root + 'directory?name=' + quote_plus(g.device_name) + '</URL>' \
'</MenuItem>' \
'</CiscoIPPhoneMenu>'
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.before_request
def before_request():
device_name = request.args.get('name', '')
if not re.search(r'(?x) ^ SEP [0-9A-F]{12} $', device_name) or \
not os.path.exists(f'{config.tftpboot_dir}/{device_name}.cnf.xml'):
return Response('Invalid device', mimetype = 'text/plain'), 500
g.device_name = device_name
g.is_79xx = re.search(r'(?x) ^ CP-79', request.headers.get('X-CiscoIPPhoneModelName', ''))
return None
@blueprint.errorhandler(Exception)
def error_handler(error):
return Response(str(error), mimetype = 'text/plain'), 500
servicesURL link
The URL accessed when the services or applications button is pressed. Example below provides this on/services
. Requires that the Asterisk manager web-interface is enabled.#!/usr/bin/python3
import os.path
import re
from urllib.parse import quote_plus
from html import escape
import requests
from lxml import etree
from flask import Blueprint, Response, request, g
import config
blueprint = Blueprint('services', __name__)
@blueprint.route('/services')
def services_menu():
xml = '<?xml version="1.0" encoding="UTF-8"?>' \
'<CiscoIPPhoneMenu>' \
'<Title>Services</Title>' \
'<MenuItem>' \
'<Name>Parked Calls</Name>' \
'<URL>' + request.url_root + 'services/parked-calls?name=' + quote_plus(g.device_name) + '</URL>' \
'</MenuItem>'
if g.is_79xx:
xml += '<Prompt>Your current options</Prompt>'
xml += '<SoftKeyItem>' \
'<Name>Exit</Name>' \
'<Position>' + ('3' if g.is_79xx else '1') + '</Position>' \
'<URL>Init:Services</URL>' \
'</SoftKeyItem>' \
'<SoftKeyItem>' \
'<Name>Select</Name>' \
'<Position>' + ('1' if g.is_79xx else '2') + '</Position>' \
'<URL>SoftKey:Select</URL>' \
'</SoftKeyItem>' \
'</CiscoIPPhoneMenu>'
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.route('/services/parked-calls')
def parked_calls():
session = requests.Session()
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'Login',
'Username': config.manager_username,
'Secret': config.manager_secret})
response.raise_for_status()
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'ParkedCalls'})
response.raise_for_status()
document = etree.fromstring(response.content)
calls = []
for element in document.findall('response/generic[@event="ParkedCall"]'):
extension = element.get('exten')
name = element.get('calleridname') or element.get('calleridnum')
calls.append((extension, name))
calls.sort(key = lambda call: call[0])
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'Logoff'})
response.raise_for_status()
xml = '<?xml version="1.0" encoding="UTF-8"?>' \
'<CiscoIPPhoneDirectory>' \
'<Title>Parked Calls</Title>'
for extension, name in calls:
xml += '<DirectoryEntry>' \
'<Name>' + escape(name) + '</Name>' \
'<Telephone>' + quote_plus(extension) + '</Telephone>' \
'</DirectoryEntry>'
if g.is_79xx:
xml += '<Prompt>Select call</Prompt>'
xml += '<SoftKeyItem>' \
'<Name>Exit</Name>' \
'<Position>' + ('3' if g.is_79xx else '1') + '</Position>' \
'<URL>' + request.url_root + 'services?name=' + quote_plus(g.device_name) + '</URL>' \
'</SoftKeyItem>' \
'<SoftKeyItem>' \
'<Name>' + ('Dial' if g.is_79xx else 'Call') + '</Name>' \
'<Position>' + ('1' if g.is_79xx else '2') + '</Position>' \
'<URL>SoftKey:Select</URL>' \
'</SoftKeyItem>' \
'<SoftKeyItem>' \
'<Name>Update</Name>' \
'<Position>' + ('2' if g.is_79xx else '3') + '</Position>' \
'<URL>SoftKey:Update</URL>' \
'</SoftKeyItem>' \
'</CiscoIPPhoneDirectory>'
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.before_request
def before_request():
device_name = request.args.get('name', '')
if not re.search(r'(?x) ^ SEP [0-9A-F]{12} $', device_name) or \
not os.path.exists(f'{config.tftpboot_dir}/{device_name}.cnf.xml'):
return Response('Invalid device', mimetype = 'text/plain'), 500
g.device_name = device_name
g.is_79xx = re.search(r'(?x) ^ CP-79', request.headers.get('X-CiscoIPPhoneModelName', ''))
return None
@blueprint.errorhandler(Exception)
def error_handler(error):
return Response(str(error), mimetype = 'text/plain'), 500
problemReportUploadURL link
The URL that the logs are uploaded to when the report problem option is selected. Example below provides this on/problem-report
. 7800 and 8800 series only.#!/usr/bin/python3
import os.path
import re
from datetime import datetime
from flask import Blueprint, Response, request, g
import config
blueprint = Blueprint('problem_report', __name__)
@blueprint.route('/problem-report', methods = ['POST'])
def problem_report():
prt_file = request.files.get('prt_file')
if prt_file is None:
return Response('Missing problem report', mimetype = 'text/plain'), 500
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
prt_file.save(f'{config.reports_dir}/{g.device_name}-{timestamp}.tar.gz')
return Response('Log saved', mimetype = 'text/plain'), 200
@blueprint.before_request
def before_request():
device_name = request.form.get('devicename', '')
if not re.search(r'(?x) ^ SEP [0-9A-F]{12} $', device_name) or \
not os.path.exists(f'{config.tftpboot_dir}/{device_name}.cnf.xml'):
return Response('Invalid device', mimetype = 'text/plain'), 403
g.device_name = device_name
return None
@blueprint.errorhandler(Exception)
def error_handler(error):
return Response(str(error), mimetype = 'text/plain'), 500
WSGI Application link
Entry point for the WSGI application. Additional configuration can be specified by theFLASK_CONFIG
environment variable.#!/usr/bin/python3
from flask import Flask, Response
import authentication
import services
import directory
import problem_report
application = Flask(__name__)
application.config.from_envvar('FLASK_CONFIG', silent = True)
application.register_blueprint(authentication.blueprint)
application.register_blueprint(services.blueprint)
application.register_blueprint(directory.blueprint)
application.register_blueprint(problem_report.blueprint)
@application.route('/')
def index():
return Response('', mimetype = 'text/plain'), 200
@application.errorhandler(Exception)
def error_handler(error):
return Response(str(error), mimetype = 'text/plain'), 500
if __name__ == '__main__':
application.run()
Application Configuration link
Parsesconfig.yml
which contains the paths and credentials for the application. A different location can be specified by the SERVICES_CONFIG
environment variable.#!/usr/bin/python3
import os
import yaml
tftpboot_dir = '/var/lib/tftpboot'
reports_dir = '/var/log/cisco'
cgi_username = 'cisco'
cgi_password = 'cisco'
manager_url = 'http://localhost:8088/mxml'
manager_username = 'asterisk'
manager_secret = 'asterisk'
config_file = os.environ.get('SERVICES_CONFIG', 'config.yml')
if os.path.exists(config_file):
with open(config_file) as file:
document = yaml.safe_load(file)
if document:
tftpboot_dir = document.get('tftpboot-dir', tftpboot_dir)
reports_dir = document.get('reports-dir', reports_dir)
cgi_username = document.get('cgi-username', cgi_username)
cgi_password = document.get('cgi-password', cgi_password)
manager_url = document.get('manager-url', manager_url)
manager_username = document.get('manager-username', manager_username)
manager_secret = document.get('manager-secret', manager_secret)