Skip to content

Instantly share code, notes, and snippets.

@thyrusg
Created March 5, 2015 02:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thyrusg/b3b2d556f71514e53527 to your computer and use it in GitHub Desktop.
Save thyrusg/b3b2d556f71514e53527 to your computer and use it in GitHub Desktop.
Generate Polycom XML directory file from FreePBX DB
#!/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=<replace-user>
SQLPASS=<replace-password>
# 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
for i in $( mysql -u $SQLUSER -p$SQLPASS -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
@VectorformIT
Copy link

VectorformIT commented Feb 7, 2018

If you want this to be more dynamic, you can have it grab the list of existing directory files in the tftpboot folder and overwrite all of them, this way the phones will update without having to factory reset them.

Below is an example of how I modified your script to do this did this:

#!/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 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=""

# Get our File List Array

declare -a filelist=($(ls /var/lib/tftpboot/ | 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/$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 '' >> $FILE
echo -e '' >> $FILE
echo -e '' >> $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" >> $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</item_list>" >> $FILE
echo -e "" >> $FILE

# Edit All Directory Files Close Loop -----------------------------------------------------------------------------------

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment