Skip to content

Instantly share code, notes, and snippets.

@chew-z
Last active January 29, 2017 11:48
Show Gist options
  • Save chew-z/9278c766422e7dcd844d to your computer and use it in GitHub Desktop.
Save chew-z/9278c766422e7dcd844d to your computer and use it in GitHub Desktop.
Wakeup script for sleepwatcher
#!/bin/bash
echo "I've been napping. Waking up now..."
echo $(date +"%c")
# Switch WiFi ON
# ADAPTER=en0
ADAPTER=$(networksetup -listallhardwareports | grep -A1 Wi-Fi | awk '/Device:/ {print $2}')
echo "$ADAPTER"
# check wifi MAC
MAC=$(/usr/local/bin/spoof-mac list --wifi)
echo "$MAC"
# Connect wifi - progressively longer delays between attempts
COUNTER=0
while true; do
let COUNTER=($COUNTER+2)
CONN=$(networksetup -getairportnetwork "$ADAPTER")
if [[ $COUNTER -gt 25 ]]
then
echo "WiFi not available. Exiting..."
break
elif [[ "$CONN" =~ .*Wi-Fi\ power\ is\ currently\ off.* ]]
then
networksetup -setairportpower "$ADAPTER" on
echo "$COUNTER: Switching WiFi power on"
sleep $COUNTER
elif [[ "$CONN" =~ .*You\ are\ not\ associated.* ]]
then
echo "$COUNTER: $CONN"
sleep $COUNTER
else
echo "$COUNTER: $CONN"
# This should trigger dnsmasq & dnscrypt-proxy restart
# via sudo launchd watching for changes in /usr/local/etc/dnsmasq/
# touch /usr/local/etc/dnsmasq/restart
# sleep 3.5
echo "www.google.com: " $(dig +tries=2 +time=3 +short A www.google.com)
break
fi
done
# Start polipo & privoxy
/usr/local/bin/brew services start privoxy
/usr/local/bin/brew services start polipo
echo "Ready to do some work!"
#!/bin/bash
echo "I've been napping. Waking up now..."
echo $(date +"%c")
# Switch WiFi ON
# ADAPTER=en0
ADAPTER=$(networksetup -listallhardwareports | grep -A1 Wi-Fi | awk '/Device:/ {print $2}')
echo "$ADAPTER"
# check wifi MAC
MAC=$(/usr/local/bin/spoof-mac list --wifi)
echo "$MAC"
# Connect wifi - progressively longer delays between attempts
COUNTER=0
while true; do
let COUNTER=($COUNTER+1)
CONN=$(networksetup -getairportnetwork "$ADAPTER")
if [[ $COUNTER -gt 12 ]]
then
echo "WiFi not available. Exiting..."
break
elif [[ "$CONN" =~ .*Wi-Fi\ power\ is\ currently\ off.* ]]
then
networksetup -setairportpower "$ADAPTER" on
echo "$COUNTER: Switching WiFi power on"
sleep $COUNTER
elif [[ "$CONN" =~ .*You\ are\ not\ associated.* ]]
then
echo "$COUNTER: $CONN"
sleep $COUNTER
else
echo "$COUNTER: - $CONN"
break
fi
done
# Start polipo & privoxy
/usr/local/bin/brew services start privoxy
/usr/local/bin/brew services start polipo
# Connect to VPN via Tunnelblick - it might get little annoying
# Alternatively VPN Details-> Configurations -> Advanced [Connecting & Disconnecting]
# "Reconnect when computer wakes up"
# osascript <<EOD
# tell application "Tunnelblick"
# connect "US California strong"
# get state of first configuration where name = "US California strong"
# repeat until result = "CONNECTED"
# delay 1
# get state of first configuration where name = "US California strong"
# end repeat
# end tell
# display notification "sleepwatcher ~/.wakeup" with title "Tunneblick connecting..."
# EOD
echo "Ready to do some work!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment