This script runs via cron once a day as the ‘asterisk’ user, and it will update the default directory template and any existing web directory for any registered polycom device in your tftproot.
Again, not exactly what you asked for but might be able to be modified for Sangoma
#!/bin/bash
# Author: Thyrus Gorges
# Date: 2015/03/04
# Modified by: Mike Stanley
# Modified Date: 2018/02/07
#The MIT License (MIT)
#Copyright (c) <2015> <Thyrus Gorges>
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
# Build a Polycom Directory XML file
# by pulling the usernames and extensions of all SIP users
# Set so we can loop on newlines instead of spaces
IFS=$'\n'
# MySQL Creds
SQLUSER="username"
SQLPASS="password"
# Create Directory Files for phones that don't have them
# This will look in your TFTP root folder for Polycom MAC address reg.cfg files to determine what phones it should create these for.
# BE SURE TO CHANGE THE TFTPBOOT LOCATION IF YOURS IS DIFFERENT
declare -a extensionlist=($(ls /var/lib/tftpboot/ | grep "reg.cfg" | cut -c 1-12))
for i in "${extensionlist[@]}"
do
if [[ ! -e /var/lib/tftpboot/polycom/contacts/"$i"-directory.xml ]];
then
echo "Creating New Directory File for $i"
touch /var/lib/tftpboot/polycom/contacts/"$i"-directory.xml
else
echo "$i already has a directory"
fi
done
# Get our File List Array
declare -a filelist=($(ls /var/lib/tftpboot/polycom/contacts/ | grep "\-directory.xml"))
# Edit All Directory Files Open Loop ---------------------------------------------------------------------------------
for i in "${filelist[@]}"
do
#Validation
echo "Updating $i"
# Declare our file
FILE="/var/lib/tftpboot/polycom/contacts/$i"
# Remove old file
rm $FILE
# ReCreate our file
touch $FILE
# Write the header of the file
# Passing the -e option to echo allows us to use tabs
echo -e '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' >> $FILE
echo -e '<!-- $RCSfile: "$i"-directory~.xml,v $ $Revision: 1.3 $ -->' >> $FILE
echo -e '<directory>' >> $FILE
echo -e "\t<item_list>" >> $FILE
# Query Database Users
# We pass -N to MySQL so it doens't print headers
# CS: Original command below, modified to remove password
# for i in $( mysql -u $SQLUSER -p$SQLPASS -N -e "use asterisk; select extension, name from users;" ); do
for i in $( mysql -u $SQLUSER -N -e "use asterisk; select extension, name from users where name not like 'ADMIN -%' and name not like 'RG -%' and name not like 'Available%' order by name asc;" ); do
# Assign Variables to each field
EXTEN=`echo $i | cut -f1`
FN=`echo $i | cut -f2 | cut -d" " -f1`
LN=`echo $i | cut -d" " -f2`
# The item tag is the beginning of a contact
echo -e "\t\t<item>" >> $FILE
echo -e "\t\t\t<ln> $LN </ln>" >> $FILE
echo -e "\t\t\t<fn> $FN </fn>" >> $FILE
echo -e "\t\t\t<ct> $EXTEN </ct>" >> $FILE
echo -e "\t\t</item>" >> $FILE
done
# Query Database Ring Groups
# We pass -N to MySQL so it doens't print headers
# CS: Original command below, modified to remove password
# for i in $( mysql -u $SQLUSER -p$SQLPASS -N -e "use asterisk; select extension, name from users;" ); do
for i in $( mysql -u $SQLUSER -N -e "use asterisk; select grpnum, description from ringgroups where description not like 'ADMIN -%' and description not like '%RG' order by description asc;" ); do
for i in $( mysql -u $SQLUSER -N -e "use asterisk; select grpnum, description from ringgroups where description not like 'ADMIN -%' and description not like '%RG' order by description asc;" ); do
# Assign Variables to each field
EXTEN=`echo $i | cut -f1`
FN=`echo $i | cut -f2 | cut -d" " -f1`
LN=`echo $i | cut -d" " -f2`
# The item tag is the beginning of a contact
echo -e "\t\t<item>" >> $FILE
echo -e "\t\t\t<ln> $LN </ln>" >> $FILE
echo -e "\t\t\t<fn> $FN </fn>" >> $FILE
echo -e "\t\t\t<ct> $EXTEN </ct>" >> $FILE
echo -e "\t\t</item>" >> $FILE
done
#_____ADD REMOTE SERVER_____
# Query Database Users
# We pass -N to MySQL so it doens't print headers
# CS: Original command below, modified to remove password
# REPLACE XXX.XXX.XXX.XXX WITH REMOTE SERVER IP
# for i in $( mysql -u $SQLUSER -p$SQLPASS -N -e "use asterisk; select extension, name from users;" ); do
for i in $( mysql -u $SQLUSER -h XXX.XXX.XXX.XXX -N -e "use asterisk; select extension, name from users where name not like 'ADMIN -%' and name not like 'RG -%' and name not like 'Available%' order by name asc;" ); do
# Assign Variables to each field
EXTEN=`echo $i | cut -f1`
FN=`echo $i | cut -f2 | cut -d" " -f1`
LN=`echo $i | cut -d" " -f2`
# The item tag is the beginning of a contact
echo -e "\t\t<item>" >> $FILE
echo -e "\t\t\t<ln> $LN </ln>" >> $FILE
echo -e "\t\t\t<fn> $FN </fn>" >> $FILE
echo -e "\t\t\t<ct> $EXTEN </ct>" >> $FILE
echo -e "\t\t</item>" >> $FILE
done
# Query Database Ring Groups
# We pass -N to MySQL so it doens't print headers
# CS: Original command below, modified to remove password
# REPLACE XXX.XXX.XXX.XXX WITH REMOTE SERVER IP
# for i in $( mysql -u $SQLUSER -p$SQLPASS -N -e "use asterisk; select extension, name from users;" ); do
for i in $( mysql -u $SQLUSER -h XXX.XXX.XXX.XXX -N -e "use asterisk; select grpnum, description from ringgroups where description not like 'ADMIN -%' and description not like '%RG' and description not like '%DID' order by description asc;" ); do
# Assign Variables to each field
EXTEN=`echo $i | cut -f1`
FN=`echo $i | cut -f2 | cut -d" " -f1`
LN=`echo $i | cut -d" " -f2`
# The item tag is the beginning of a contact
echo -e "\t\t<item>" >> $FILE
echo -e "\t\t\t<ln> $LN </ln>" >> $FILE
echo -e "\t\t\t<fn> $FN </fn>" >> $FILE
echo -e "\t\t\t<ct> $EXTEN </ct>" >> $FILE
echo -e "\t\t</item>" >> $FILE
done
#_____END REMOTE SERVER_____
# Write file endings
echo -e "\t</item_list>" >> $FILE
echo -e "</directory>" >> $FILE
# Edit All Directory Files Close Loop -----------------------------------------------------------------------------------
done