Creating a FreePBX Polycom directory for all of your users can be a frustrating and manual process. The goal of this article is to make that process much much easier. Once complete, you should have a central directory in all of your Polycom phones that shows all other employee extensions, and that central directory will update hourly from FreePBX’s database.
First, log into your FreePBX via SSH as root.
Create a new application in /usr/local/sbin called polycom-dir.sh:
nano -w /usr/local/sbin/polycom-dir.sh
Credit where credit is due – I stole the base of this script from Thyrus Gorges – he did the bulk of the heavy lifting, and I simply modified the script a bit for easier use with FreePBX. You can see Thyrus’s original post here.
Copy and paste the following into the polycom-dir.sh file:
#!/bin/bash # Author: Thyrus Gorges # Date: 2015/03/04 #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=root SQLPASS="" # Declare our file FILE=/tftpboot/000000000000-directory.xml # 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: 000000000000-directory~.xml,v $ $Revision: 1.3 $ -->' >> $FILE echo -e '<directory>' >> $FILE echo -e "\t<item_list>" >> $FILE # Query Database # 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;" ); 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 # Write file endings echo -e "\t</item_list>" >> $FILE echo -e "</directory>" >> $FILE
Note that this script assumes that you are using the FreePBX default MySQL credentials of ‘root’ with no password. If you are using different MySQL credentials, modify the script accordingly.
Once you have everything copied in, save and exit by doing CTRL+X followed by Y.
Next, make your new script executable:
chmod +x /usr/local/sbin/polycom-dir.sh
Run the script as ‘asterisk’ user:
runuser -l asterisk -c '/usr/local/sbin/polycom-dir.sh'
Check /tftpboot for 000000000000-directory.xml file:
ls -la /tftpboot |grep xml
The 000000000000-directory.xml file should have a very recent time and date stamp.
Everytime this script runs, it will overwrite the previous version.
Next, let’s tell FreePBX to run it once an hour.
nano -w /etc/crontab
Append the following line to the bottom of the crontab file:
0 * * * * asterisk /usr/local/sbin/polycom-dir.sh
CTRL+X followed by Y to save and exit.
That’s it! Reboot your Polycom phones, and they should now pick up this new FreePBX Polycom directory.