I use a Cisco SPA112 for VoIP lines and it has been acting up lately, occasionally dropping incoming calls when the handset is picked up.
Debugging the issue has been tricky because after applying new settings + a reboot, the problem doesn’t show up again until the device has been on for at least a week.
Instead of waiting for a failure and potentially missing some calls, I wrote a bash script to preemptively reboot the device.
#!/bin/bash
#
# Reboot a Cisco SPA112 using its web ui.
# Tested on firmware v1.4.1 (002).
#
# Written by Ian Wilson <ian@iswilson.com> on 2017-03-07
DEVICE_IP=192.168.1.42
USER=admin
PASSWORD=hunter2
# Port of the en_value() javascript function from Login.asp on the SPA112.
encode_password () {
# Append the password length (0-padded) to the password.
local password_hash="$(printf "%s%02d" ${PASSWORD} ${#PASSWORD})"
# Repeat the string until it is longer than 64 characters.
while [[ ${#password_hash} -lt 64 ]]; do
password_hash+="${password_hash}"
done
# Return a MD5 hash of the first 64 characters.
echo -n "${password_hash:0:64}" | md5sum
}
# Submit the login form.
echo -n "Logging in..."
login_response=$(curl \
--silent \
--data "submit_button=login&keep_name=0&enc=1&user=${USER}&pwd=$(encode_password)" \
"http://${DEVICE_IP}/login.cgi")
# Parse the response to see if login was successful.
if [[ ${login_response} =~ "var session_key" ]]; then
echo "success!"
else
echo "failed."
exit 1
fi
# Get session key from the response.
echo -n "Grabbing session key..."
session_key="$(echo "${login_response}" | sed --silent --regexp-extended "s/var session_key='([0-9a-f]{32})';/\1/p")"
# Check if the session key is empty.
if [[ -z "${session_key}" ]]; then
echo "failed."
exit 1
else
echo "success!"
fi
# Submit the reboot form.
echo -n "Sending reboot command..."
reboot_response=$(curl \
--silent \
--data "submit_button=Reboot&gui_action=Apply&need_reboot=1&session_key=${session_key}" \
"http://${DEVICE_IP}/apply.cgi;session_id=${session_key}")
# Parse the response to see if a reboot was started.
if [[ ${reboot_response} =~ "You will be returned to the Login page in a few minutes" ]]; then
echo "success!"
else
echo "failed."
exit 1
fi
# Exit.
exit 0
The script uses curl to log into the device’s web ui and initiate a reboot. I have this set up to run daily in the wee hours of the morning using cron.