Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Created November 1, 2021 15:57
Show Gist options
  • Save chtzvt/4b5966430744473fc25f09cbee7c5d22 to your computer and use it in GitHub Desktop.
Save chtzvt/4b5966430744473fc25f09cbee7c5d22 to your computer and use it in GitHub Desktop.
GrandStream HT801/HT802 dynamic DNS autoupdater.
#!/bin/bash
EMAIL="you@example.com"
GS_HOSTNAME="your-grandstream-hostname"
STATUS_FILE='/tmp/.voip_ip_stat'
touch $STATUS_FILE
timeout 3 bash -c "printf '@' | telnet -e \@ $GS_HOSTNAME 22" 2>&1 >/dev/null
host_alive=$?
grep "gs_host_down" $STATUS_FILE 2>&1 >/dev/null
last_error_gs_down=$?
if [[ $host_alive != 0 && $last_error_gs_down != 0 ]];
then
echo "[`date`] ERROR updating grandstream nat IP: unable to reach device"
echo "gs_host_down" >$STATUS_FILE
exit 1
fi
grep "gs_host_down_extended" $STATUS_FILE 2>&1 >/dev/null
gs_down_notice_sent=$?
if [[ $host_alive != 0 && $last_error_gs_down == 0 ]];
then
echo "[`date`] ERROR updating grandstream nat IP: device unreachable for an extended time period"
echo "gs_host_down_extended" >$STATUS_FILE
if [[ $gs_down_notice_sent == 1 ]];
then
echo "This is the VOIP management service at NOPE HQ.
The VOIP device at $GS_HOSTNAME has been unreachable for an extended period of time.
Please fix this issue at your earliest convenience.
Best regards,
NOPE Nanomachines
[generated: `hostname` `date`]
" | mailx -s 'NOPE VOIP Management Notice: Equipment Offline' $EMAIL
fi
exit 1
fi
record_content=`curl -s http://whatismyip.akamai.com/`
fetch_succeeded=$?
if [[ $fetch_succeeded != 0 ]];
then
echo "[`date`] ERROR updating grandstream nat IP: unable to fetch WAN IP ($record_content)"
echo "ipcheck_wan_fail" >$STATUS_FILE
exit 1
fi
current_ip=`printf "config\nget 101\nexit\nexit" | sshpass -f /home/jobs/scripts/gspw ssh admin@$GS_HOSTNAME 2>/dev/null | grep 101 | cut -d '=' -f2 | xargs`
fetch_succeeded=$?
if [[ $fetch_succeeded != 0 ]];
then
echo "[`date`] ERROR updating grandstream nat IP: unable to fetch NAT IP from device ($current_ip)"
echo "ipcheck_gs_fail" >$STATUS_FILE
exit 1
fi
if [[ $record_content == $current_ip ]];
then
echo "[`date`] OK updating grandstream nat IP: device setting matches detected value (gs:$current_ip, wan:$record_content)"
rm -f $STATUS_FILE
exit 0
fi
xaction=`printf "config\nset 101 $record_content\ncommit\nexit\nexit" | sshpass -f /home/jobs/scripts/gspw ssh admin@$GS_HOSTNAME 2>/dev/null`
update_succeeded=$?
updated_cfg_ip=`echo $xaction | grep 101 | cut -d '=' -f2 | xargs`
echo $xaction | grep 'Changes are commited.' >/dev/null
commit_successful=$?
if [[ $update_succeeded != 0 || $commit_successful != 0 ]];
then
echo "[`date`] ERROR updating grandstream nat IP! $xaction (gs:$updated_cfg_ip wan:$record_content)"
echo "gs_update_config_fail" >$STATUS_FILE
echo "This is the VOIP management service at NOPE HQ.
The automated NAT IP update job has failed for $GS_HOSTNAME.
Debugging information:
WAN IP: $record_content
Device IP record: $current_ip
Attepmpted update: $updated_cfg_ip
Transaction log: $xaction
Please fix this issue at your earliest convenience.
Best regards,
NOPE Nanomachines
[generated: `hostname` `date`]
" | mailx -s 'NOPE VOIP Management Notice: Job Failure' $EMAIL
exit 1
else
echo "[`date`] Successfully updated grandstream nat IP"
rm -f $STATUS_FILE
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment