Skip to main content

Shell Scrips

Sometimes it is easier to write a short shell script that reads the PBX database and prints it on the screen, or does other functions with it. This page shows some examples of useful functions.

MAC Overview

The first shell script shows how to list the MAC addresses that have been assigned to extensions.

#!/bin/bash 
function get_xml()
{
gawk -v tag=$1 'BEGIN{regex="<" tag ">([^<]*)</" tag ">";}{ match($0, regex, m); for(i = 1;; i++) { if(!(i in m)) break; printf("%s\n",m[i]);}}' $2
}

for mac in macs/*.xml
do
name=${user:5}
# only the name
user=$(get_xml user $mac)
adr=$(get_xml adr $mac)
alias=$(get_xml alias users/$user.xml)
domain=$(get_xml domain users/$user.xml)
echo $adr $(get_xml name user_alias/$alias.xml)@$(get_xml name domains/$domain.xml)
done

Count Extensions

This script count how many extensions are in each domain.

#!/bin/bash
# Show the passwords of all users:
function get_xml()
{
gawk -v tag=$1 'BEGIN{regex="<" tag ">([^<]*)</" tag ">";}{ match($0, regex, m); for(i = 1;; i++) { if(!(i in m)) break; printf("%s\n",m[i]);}}' $2
}

for dom in domains/*.xml; do
name=${dom:8}
# only the name
idx=${name%.xml}
# only the number
dn=$(get_xml name $dom)
if [ ! -z "$dn" ]; then
count=0
for ext in users/*.xml; do
domain=$(get_xml domain $ext)
if [ "$domain" == "$idx" ]; then
type=$(get_xml type $ext)
if [ $type == "extensions" ]; then
count=$[$count+1]
fi
fi
done
echo $dn $count
fi
done

Show Email Addresses

This script shows the various email addresses .

#!/bin/bash
# Show the emails of the users
function get_xml()
{
gawk -v tag=$1 'BEGIN{regex="<" tag ">([^<]*)</" tag ">";}{ match($0, regex, m); for(i = 1;; i++) { if(!(i in m)) break; printf("%s\n",m[i]);}}' $2
}

for xml in users/*.xml; do
n=$(get_xml alias $xml)
name=$(get_xml name user_alias/$n.xml)
d=$(get_xml domain $xml)
domain=$(get_xml name domains/$d.xml)
email=$(get_xml email_cdr $xml)
[ -n "$email" ] && echo $name@$domain: $email
type=$(get_xml type $xml)
if [ $type = extensions ]; then
id=$(get_xml id $xml)
for t in email_address email_recadr wakeup_fail_email; do
email=$(get_xml $t extensions/$id.xml)
[ -n "$email" ] && echo $name@$domain: $email
done
fi
done
for xml in domains/*.xml; do
domain=$(get_xml name $xml)
for t in email_cdr e911_emails billing_email; do
email=$(get_xml $t $xml)
[ -n "$email" ] && echo $domain: $email
done
done