Skip to content

Instantly share code, notes, and snippets.

@n7st
Last active June 14, 2016 07:30
Show Gist options
  • Save n7st/31a1f902b70206267d6107a1e758a5cd to your computer and use it in GitHub Desktop.
Save n7st/31a1f902b70206267d6107a1e758a5cd to your computer and use it in GitHub Desktop.
Scripts to sleep and wake machines if I'm at home
#!/usr/bin/env bash
set -uf -o pipefail;
usage="./suspend.sh 22";
: ${1?"Usage: $0 SSH port ($usage)"}
inbound=$(netstat -n --protocol inet | grep ":$1");
if [[ "$inbound" = "" ]];
then
/usr/sbin/pm-suspend;
else
exit 1;
fi
#!/usr/bin/env bash
set -euf -o pipefail;
usage="./wake.sh 192.168.1.98 ue0 xx:xx:xx:xx:xx:xx 192.168.1.255";
phone_connected=0;
: ${1?"Usage: $0 phone's LAN IP address ($usage)"}
: ${2?"Usage: $0 network interface (eth0 etc) ($usage)"}
: ${3?"Usage: $0 comma separated machine ethernet addresses ($usage)"}
: ${4?"Usage: $0 network broadcast address ($usage)"}
scan_cache=$(/usr/local/bin/arp-scan -l); # Build a new arp cache
[[ $(/usr/sbin/arp -i $2 $1 | grep 'expires') ]] && phone_connected=1;
if [[ $phone_connected -eq 0 ]];
then
echo "Phone isn't connected";
exit 1;
fi
# else continue and wake things up
IFS=',' read -ra addresses <<< "$3";
for ethernet in "${addresses[@]}";
do
/usr/local/bin/wakeonlan -i $4 $ethernet;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment