-
Notifications
You must be signed in to change notification settings - Fork 0
/
wake-host
executable file
·132 lines (110 loc) · 2.86 KB
/
wake-host
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#! /bin/bash
usage="[--ssh-identity identity-file] host mac-address
wake 'host' with MAC address 'mac-address', using SSH identity given by
'identity-file'.
SSH is used to test that the host is up after trying to wake it."
# For Mac's, ping cannot be used to see if the host is up, because the
# machine responds to pings even when asleep.
macprefixes="/usr/share/nmap/nmap-mac-prefixes"
set -eu
set -o pipefail
self="${BASH_SOURCE[0]}"
SCRIPTDIR=$(cd "$(dirname "$self")" && pwd)
argv0="$(basename "$self")"
function usage {
[ "${*-}" ] && echo "$argv0: Error: $*" 1>&2
echo "Usage: $argv0: $usage" 1>&2
exit 1
}
function errordie {
[ "${*-}" ] && echo "$argv0: Error: $*" 1>&2
exit 1
}
for cmd in ether-wake start_caffeinate_daemon; do
if ! type -p $cmd > /dev/null; then
errordie command $cmd not found
fi
done
debug=
function d {
# "echo" and optionally execute the arguments to this function
# if --debug is given to the main script, then just echo the
# arguments.
#
# This can be used to run scripts with --debug so you can see what
# commands they would execute. This is good for script that do
# possibly dangerous things. Or operations which would take a
# long time to execute and you want to make sure everything is OK before
# trying it out.
if [ "$debug" ]; then
echo "would do: $*"
else
echo "+ $*"
"$@"
fi
}
host=
mac=
sshid=
while [ $# -gt 0 ]; do
case $1 in
--debug) debug=$1 ;;
--ssh-identity)
[ $# -lt 2 ] && usage companion to --ssh-identity missing
shift
sshid=$1
;;
-*) usage ;;
*) [ $# -ne 2 ] && usage missing arguments
host=$1
mac=$2
break
;;
esac
shift
done
if [ ! -f "$macprefixes" ]; then
echo $argv0: $macprefixes does not exist, install nmap
exit 1
fi
ar=($mac)
prefix="$(echo ${ar[0]:0:8} | sed 's/://g')"
apple=
if grep -i $prefix $macprefixes | grep -iq apple; then
apple=xxx
fi
if [ -e /sys/class/net/lan ]; then
dev=lan
elif dev=$(/sbin/route -n | grep "^0.0.0.0" | rev | cut -d' ' -f1 | rev); then
:
else
echo $argv0: could not find ethernet device
exit 1
fi
ssh=$(which ssh)
[ "$sshid" ] && ssh="$ssh -i $sshid"
if $ssh $host /bin/date > /dev/null; then
echo $0: $host already awake
exit 0
fi
echo $0: using device $dev and MAC $mac
sudo ether-wake -i $dev $mac
sleep 30
if $ssh $host /bin/date > /dev/null; then
echo $0: successfully woke $host
if [ "$apple" ]; then
# Keep the host awake for 5 minutes, so whatever we wanted to do
# has time to start
seconds=$(( 5 * 60 ))
echo $0: start caffeinate daemon for $seconds seconds
if ! $ssh $host start_caffeinate_daemon $seconds; then
echo $0: failed to start caffeinate daemon
exit 1
else
exit 0
fi
fi
else
echo $0: could not wake $host
exit 1
fi