FreePBX XML Directory Setup

Logicom USA  > Uncategorized >  FreePBX XML Directory Setup
0 Comments

🗂️ Creating an Auto-Updating Company Directory in FreePBX

Setting up a company directory in FreePBX makes it easier for larger organizations to locate users by extension. This guide shows you how to automatically generate and update an XML directory every hour using a custom script.


🛠 Step 1: Create the Script

    1. SSH into your FreePBX server as root.

    2. Create a new script file:

      nano -w /usr/local/sbin/xml_directory.sh
    3. Paste the following script into the file. (Credit to Thyrus Gorges; script modified from original.)


      #!/bin/bash
      # Author: Thyrus Gorges
      # Date: 2015/03/04

      #The MIT License (MIT)

      #Copyright (c) <2015>

      #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 an XML Directory file that can be used for Polycom, Yealink, ClearlyIP, Sangoma as well as other phones.
      # 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/company_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 '' >> $FILE
      echo -e '' >> $FILE
      echo -e '' >> $FILE
      echo -e "\t" >> $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" >> $FILE
      echo -e "\t\t\t $LN " >> $FILE
      echo -e "\t\t\t $FN " >> $FILE
      echo -e "\t\t\t $EXTEN " >> $FILE
      echo -e "\t\t" >> $FILE
      done

      # Write file endings
      echo -e "\t" >> $FILE
      echo -e "" >> $FILE

(Script contents are available upon request—omitted here for clarity and copyright reasons.)

  1. Save and exit with CTRL+X, then Y, then Enter.


🔐 Step 2: Make the Script Executable

chmod +x /usr/local/sbin/xml_directory.sh

▶️ Step 3: Test the Script

Run the script as the Asterisk user:

runuser -l asterisk -c '/usr/local/sbin/xml_directory.sh'

Check that the directory file was created:

ls -la /tftpboot | grep xml

You should see company_directory.xml listed.


🕒 Step 4: Automate with Cron

  1. Edit the crontab file:

    nano -w /etc/crontab
  2. Add this line at the bottom:

    0 * * * * asterisk /usr/local/sbin/xml_directory.sh
  3. Save and exit.

This runs the script every hour on the hour.


📞 Final Step: Configure Your Phones

Direct your phones (e.g., Yealink, Polycom, Sangoma) to point to the XML file at /tftpboot/company_directory.xml. Refer to your phone model's documentation for instructions on setting up remote directories.


Leave a Reply

Your email address will not be published. Required fields are marked *