Skip to content

Instantly share code, notes, and snippets.

@mbierman
Last active December 3, 2018 20:21
Show Gist options
  • Save mbierman/b1b6e36f5ef5878c23f467660ebb8f95 to your computer and use it in GitHub Desktop.
Save mbierman/b1b6e36f5ef5878c23f467660ebb8f95 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Configure/Initialize
APIKEY="$(cat ~/Documents/Applications/bin/arrisdata.txt | grep API | cut -f2 -d "=" )"
USER="$(cat ~/Documents/Applications/bin/arrisdata.txt | grep USER | cut -f2 -d "=" )"
PASS="$(cat ~/Documents/Applications/bin/arrisdata.txt | grep PASS | cut -f2 -d "=" )"
tries=1
SECONDS=0
run="null"
begin=`date +%s`
dur=59
permlog=/tmp/com.arrisreboot.log
dns="FALSE"
showme="FALSE"
silent="FALSE"
dryrun="FALSE"
for i in "$@" ; do
if [ "$i" = "silent" ]; then
silent="TRUE"
elif [ "$i" = dns ]; then
dns="TRUE"
elif [ "$i" = "showme" ]; then
showme="TRUE"
elif [ "$i" = "dryrun" ]; then
dryrun="TRUE"
else
echo -e "\nValid options are: \n * dns (do not sleep)\n * showme (open web page)\n * dryrun (don't actually reboot modem)\n * silent (don't push notification)\n"
exit
fi
done
echo dryrun is:$dryrun
function convertsecs() {
((h=${runtime}/3600))
((m=(${runtime}%3600)/60))
((s=${runtime}%60))
printf "%02d:%02d:%02d\n" $h $m $s
}
function ready() {
while [ "$test" != "$1" ]
do
if [ "$1" = "DOCTYPE" ]; then
echo "$1"
test=$(curl http://192.168.0.1 | grep -aho 'DOCTYPE')
elif [ "$1" = "HTTP/2" ]; then
test=$(curl --connect-timeout 5000 -I https://cnn.com | grep -ho 'HTTP/2')
else
echo uh oh.
exit
fi
((tries+=1))
if [ "$test" != "$1" ]; then
sleep 20
fi
done
echo "starting Ready"
if [ "$dns" != "TRUE" ]; then
pmset -a displaysleep 10 disksleep 10 sleep 15 womp 1
else
echo no sleep
fi
}
# Begin MAIN
ready "DOCTYPE"
if [ "$dryrun" != "TRUE" ]; then
echo "working..."
curl --user $USER:$PASS -d "loginChallenge=o8F9kmPmC75wzc2H3bWCO3Fxi3dYKGFb&loginUsername=michaelb&loginPassword=eAevjIfUq6okHYC7RReq" -H "Content-Type: text/html" -v http://192.168.0.1/goform/login & -v http://192.168.0.1/goform/RgConfiguration.pl -d "GetNonce=R0T5QUiNN1hPNqjHKtqNsHEFnuIoUIi&EEE=0&Rebooting=1" -X POST -H "Content-Type: text/html" -H "content-length: 58"
sleep 30
else
echo "DryRun..."
fi
if [ "$dns" = "FALSE" ]; then
sudo ifconfig en0 down
sudo ifconfig en1 down
sleep 10
dscacheutil -flushcache;sudo killall -HUP mDNSResponder;echo DNS cleared.
sleep 30
sudo ifconfig en0 up
sudo ifconfig en1 up
else
echo "No sleep for you..."
fi
#Test LAN
echo "LAN up?"
ready "DOCTYPE"
#Test WAN
echo "WAN up?"
ready "HTTP/2"
# we're done!
end=`date +%s`
runtime=$((end-begin))
runtime=$(convertsecs $runtime)
ts=$(date +"%m/%d @%H:%M")
if [ $tries -eq 1 ]; then
output=">>> Completed in $runtime (${tries} try) <<<"
else
output=">>> Completed in $runtime ($tries tries) <<<"
fi
echo output is: "$output"
if [ "$silent" = "FALSE" ]; then
curl https://api.pushbullet.com/api/pushes -u $APIKEY -d type=note -d title="Arris restarted $ts, tries: $tries ($runtime)" -d body="restarted at $(date) in $tries tries in $runtime" -X POST > /dev/null
else
echo "Shhh"
fi
echo "$output"
echo "$output" >> $permlog
if [ "$dns" = "FALSE" ]; then
osascript -e 'tell app "System Events" to sleep'
fi
if [ $tries -eq 1 ];then
output=">>> Completed in $runtime (${tries} try) <<<"
else
output=">>> Completed in $runtime ($tries tries) <<<"
fi
if [ "$dns" = "FALSE" ]; then
pmset -a displaysleep 10 disksleep 10 sleep 15 womp 1
# sleep 300
else
echo "no sleep for you"
fi
echo "$output"
echo "$output" >> $permlog
if [ "$showme" = "TRUE" ]; then
open http://192.160.0.1
fi
# sudo launchctl unload /Library/LaunchDaemons/com.arrisreboot.plist; sleep 3; sudo launchctl unload /Library/LaunchDaemons/com.arrisreboot.plist; sudo launchctl load -w /Library/LaunchDaemons/com.arrisreboot.plist; sudo launchctl load -w /Library/LaunchDaemons/com.arrisreboot.plist
@mbierman
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment