Restart unregistered VoIP phone via PoE

Have you tried turning it off and on again? Sad but true, it really helps sometimes. In my particular case I have this Gigaset Maxwell 10 VoIP phone that sometimes loses its SIP registration on my FreePBX server. Manually rebooting the phone remediates the problem, but let’s automate that…

The following script runs every 10 minutes from cron on the FreePBX server. It queries the state of the pjsip endpoint and when it is unavailable, an on-the-fly script is made that is sent to the switch and executed there.

Alltough I made this script specifically for my Ubiquiti UniFi PoE switch, you can easily adapt it for any other switch model/vendor that has a telnet/ssh CLI. To avoid using passwords for SSH to the UniFi switch, I added the public SSH key of the user on the FreePBX server to the UniFi controller.

#!/bin/sh
LOGFILE=/var/log/check_endpoints.log

if (rasterisk -x "pjsip show endpoint 201" | grep Unavailable) then 

echo `date +"%Y-%m-%d %T"` endpoint 201 unavailable, restarting via PoE >> $LOGFILE

USWSCRIPT=./uswscript.sh
cat > $USWSCRIPT << FOE
telnet localhost << EOF
enable
configure
interface 0/7
poe opmode shutdown
exit
interface 0/7
poe opmode auto
exit
EOF
FOE

scp $USWSCRIPT ubnt@10.10.10.12:$USWSCRIPT

ssh ubnt@10.10.10.12 << EOF
sleep 1
chmod +x ./uswscript.sh
sh -c ./uswscript.sh
EOF

echo `date +"%Y-%m-%d %T"` endpoint 201 restart command given >> $LOGFILE

fi

Leave a Reply

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