Created
March 7, 2017 23:36
-
-
Save iswilson/ba8cb249f38a0f068d4df3ee50751aa7 to your computer and use it in GitHub Desktop.
Bash script to reboot a Cisco SPA112 using its web ui.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Reboot a Cisco SPA112 using its web ui. | |
# Tested on firmware v1.4.1 (002). | |
# | |
# Written by Ian Wilson <[email protected]> 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment