-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwakeup
executable file
·84 lines (74 loc) · 2.22 KB
/
wakeup
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
#!/bin/ksh
#
# wakeup - wakes up a host by sending Wake-On-Lan magic packet with ether-wake
#
# Copyright (C) 2012 Orion Poplawski <orion@cora.nwra.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# root generally runs this via gewake and we don't want to wait
[ `id -u` -eq 0 ] && QUIET=-q
# The list of ethernet macaddrs for the hosts
etherhostlist=/nfs/local/etc/wakeup
# Parse options
while getopts "q" opt
do
case $opt in
q) QUIET=-q;;
\?)
echo "Use: wakeup [-q] hostname" >&2
echo " -q quiet - don't wait for the host to come up" >&2
exit 1
;;
esac
done
shift $(($OPTIND - 1))
host=$1
# Declare the associative array for the ethernet macaddrs
typeset -A ether
# Source the list of ethernet addresses
. ${etherhostlist}
# Set the ethernet address
macaddr=${ether[$host]}
# Quit if unknown
if [ -z "$macaddr" ]
then
echo "ERROR: Unknown host $host, add to $wakeuphosts!" 1>&2; exit 1
fi
if [ `id -u` -ne 0 ]
then
sudo=sudo
fi
#Send it out all interfaces to be safe
/sbin/ip link | awk -F: '/BROADCAST/ { print $2 }' | while read if x
do
$sudo /sbin/ether-wake -i $if $macaddr 2>&1 | grep -vF 'sendto: Network is down'
done
#Non-root, wait for the machine to come up
if [ -z "$QUIET" ]
then
echo -n "Waiting for $host to come up."
i=0
while [ $i -lt 60 ]
do
# Try to connect to ssh to see if up
perl -e "use IO::Socket; IO::Socket::INET->new(Proto=>'tcp',PeerAddr=>$host,PeerPort=>'ssh') or exit(1);" && {
echo " UP!"
exit
}
echo -n "."
let i++
sleep 1
done
echo -e " FAILED!\nGave up waiting for $host to come up."
fi