Script to search for new handsets and automatically fill users.conf

Usage sample:
1. Fill arp table:
#nmap -sP {yuor_ip}/{your_mask}
2. send arp table to script:
#arp -an |


usersconf=/etc/asterisk/users.conf  # Asterisk users.conf file
password=admin                           # Phone admin password for remote reboot
vendormac=00:0b                         # Vendor mac address prefix (00:0b - Grandstream)
numdigs=2                                   # Number of digits in internal phone numbers 2 - 00 3 - 000 4 - 0000
secret=cnfywbz                              # sip password 
num=1                                         # start phone number

cp ${usersconf} ${usersconf}.old     # create reserve copy
printf -v numt "%.*u" ${numdigs} ${num}
while read str ;
mac=$(echo -n ${str} | grep -o -E '([0-9a-z]{2}:){5}[0-9a-z]{2}' | grep "${vendormac}" | sed 's|:||g' )
if [ "${mac}" ] && [ ! "$(grep ${mac} ${usersconf})" ] ; then
    ip=$(echo -n ${str} | grep -o -E '([0-9]{1,3}\.){3}[0-9a-z]{1,3}')
    echo New phone found mac=${mac} ip=${ip}
    while [ "$(grep "\[${numt}\]" ${usersconf} )" ] ;
	echo Number ${numt} already exist. Skipping.
	let num++
	printf -v numt "%.*u" ${numdigs} ${num}
    echo Assign to internal number ${numt}
    ( echo "[${numt}]"
    echo macaddress = ${mac}
    echo autoprov = yes
    echo secret = ${secret}
    echo fullname = ${numt}
    echo ) >>${usersconf}
    echo Reload asterisk config
    asterisk -rx "config reload ${usersconf}" # Informs asterisk that changed config file 
    echo Reboot phone
    gsreboot ${password} ${ip} # Remote reboot phone for autoprovisioning
    let num++
    printf -v numt "%.*u" ${numdigs} ${num}
    echo ---

gsreboot see: Bash script to remote reboot some Grandstream phones

Created by: laigor, Last modification: Fri 28 of Mar, 2014 (13:20 UTC)
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+