Skip to content

Instantly share code, notes, and snippets.

@JJTech0130
Last active February 2, 2025 20:48
Show Gist options
  • Save JJTech0130/58fa505b3b88fc5a3e3d033dc8caf3d5 to your computer and use it in GitHub Desktop.
Save JJTech0130/58fa505b3b88fc5a3e3d033dc8caf3d5 to your computer and use it in GitHub Desktop.
Script to reboot Verizon 5G Home Internet router automatically
#!/bin/bash
# Configuration
password="XTYTH3CP2" # Set your password here
timeout_duration=10 # Set timeout duration in seconds
# Change into a temporary directory, storing the current directory
current_dir=$(pwd)
cd "$(mktemp -d)" || exit
# Function to get the current public IP address
get_public_ip() {
ip=$(curl -s https://api.ipify.org) # Fetch current public IP using ipify
echo "$ip"
}
# Function to increment the IP address by 1
increment_ip() {
ip=$1
# Split IP into its parts (assumes IPv4)
IFS='.' read -r -a ip_parts <<<"$ip"
# Add 1 to the last part of the IP
ip_parts[3]=$((ip_parts[3] + 1))
new_ip="${ip_parts[0]}.${ip_parts[1]}.${ip_parts[2]}.${ip_parts[3]}"
echo "$new_ip"
}
# Function to decrement the IP address by 1
decrement_ip() {
ip=$1
# Split IP into its parts (assumes IPv4)
IFS='.' read -r -a ip_parts <<<"$ip"
# Subtract 1 from the last part of the IP
ip_parts[3]=$((ip_parts[3] - 1))
new_ip="${ip_parts[0]}.${ip_parts[1]}.${ip_parts[2]}.${ip_parts[3]}"
echo "$new_ip"
}
# Function to get the loginToken from the loginStatus.cgi endpoint
get_login_token() {
ip=$1
response=$(timeout $timeout_duration curl -s -w "%{http_code}" "https://$ip/loginStatus.cgi" --insecure -o response.json)
http_code=$(echo "$response" | tail -n1) # Extract HTTP status code
if [[ "$http_code" -eq 403 ]]; then
echo "Error: 403 Forbidden response from loginStatus.cgi. Exiting."
exit 1
elif [[ "$http_code" -ne 200 ]]; then
echo "Error: Unexpected HTTP response $http_code from loginStatus.cgi. Exiting."
exit 1
fi
login_token=$(jq -r '.loginToken' response.json)
# Check if login_token is empty or the response is invalid
if [[ -z "$login_token" || "$login_token" == "null" ]]; then
echo "Error: Failed to retrieve login token. Exiting."
return 1
fi
echo "$login_token"
return 0
}
# Function to generate the SHA512 of (token + SHA512(MD5(password)))
generate_sha512_password() {
md5_password=$(echo -n "$password" | md5sum | awk '{print $1}')
sha512_md5_password=$(echo -n "$md5_password" | sha512sum | awk '{print $1}')
token=$1
combined="$token$sha512_md5_password"
sha512_password=$(echo -n "$combined" | sha512sum | awk '{print $1}')
echo "$sha512_password"
}
login() {
ip=$1
login_token=$2
sha512_password=$(generate_sha512_password "$login_token")
# Username is probably SHA512("admin")?
response=$(timeout $timeout_duration curl -s -w "%{http_code}" -c cookies.txt "https://$ip/login.cgi" \
--data-raw "luci_username=edbd881f1ee2f76ba0bd70fd184f87711be991a0401fd07ccd4b199665f00761afc91731d8d8ba6cbb188b2ed5bfb465b9f3d30231eb0430b9f90fe91d136648&luci_password=$sha512_password&luci_view=Desktop&luci_token=$login_token&luci_keep_login=0" \
--insecure -o response.json)
http_code=$(echo "$response" | tail -n1) # Extract HTTP status code
if [[ "$http_code" -eq 403 ]]; then
echo "Error: 403 Forbidden response from login.cgi. Exiting."
exit 1
elif [[ "$http_code" -ne 302 ]]; then
echo "Error: Unexpected HTTP response $http_code from login.cgi. Exiting."
exit 1
fi
echo "Login successful, 302 redirect received."
}
# Function to fetch the login status and new token
get_login_status_and_token() {
ip=$1
response=$(timeout $timeout_duration curl -s -w "%{http_code}" -b cookies.txt "https://$ip/loginStatus.cgi" --insecure -o response.json)
http_code=$(echo "$response" | tail -n1)
if [[ "$http_code" -eq 403 ]]; then
echo "Error: 403 Forbidden response from loginStatus.cgi. Exiting."
exit 1
elif [[ "$http_code" -ne 200 ]]; then
echo "Error: Unexpected HTTP response $http_code from loginStatus.cgi. Exiting."
exit 1
fi
# Extract the new token from the response
new_token=$(jq -r '.token' response.json)
echo "$new_token"
}
# Function to initiate the reboot
reboot_router() {
ip=$1
token=$2
response=$(timeout $timeout_duration curl -s -w "%{http_code}" -b cookies.txt -X POST "https://$ip/apply_abstract.cgi" \
--data "token=$token&action=reboot" --insecure -o response.json)
http_code=$(echo "$response" | tail -n1)
if [[ "$http_code" -ne 200 ]]; then
echo "Error: Failed to initiate reboot. HTTP response $http_code. Exiting."
exit 1
fi
echo "Router reboot initiated successfully."
}
# Main script execution
current_ip=$(get_public_ip) # Get current public IP address
new_ip=$(increment_ip "$current_ip") # Increment IP address
decrement_ip_address=$(decrement_ip "$current_ip") # Decrement IP address
echo "Trying the incremented IP: $new_ip"
if ! login_token=$(get_login_token "$new_ip"); then
echo "Failed to get login token from $new_ip. Trying the decremented IP: $decrement_ip_address"
if ! login_token=$(get_login_token "$decrement_ip_address"); then
echo "Failed to retrieve login token from both IP addresses. Exiting."
exit 1
fi
# If the decremented IP works, update new_ip to use it
new_ip="$decrement_ip_address"
fi
echo "Login token retrieved: $login_token"
login "$new_ip" "$login_token" # Perform login action
new_token=$(get_login_status_and_token "$new_ip") # Get new token from loginStatus.cgi
echo "New token retrieved: $new_token"
reboot_router "$new_ip" "$new_token" # Perform reboot action
# Change back to the original directory
cd "$current_dir" || exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment