blob: 0504cf1c0aa75b5a4b43dd8a2b17e41229bcc109 (
plain)
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/sh /etc/rc.common
# Copyright (C) 2009 OpenWrt.org
NAME='etherwake'
START=60
PROGRAM=''
start()
{
local searchlist=''
local section=''
local value=''
config_load "${NAME}"
# check for available program
config_get searchlist 'setup' 'pathes'
PROGRAM=$(search_program "${searchlist}")
[ -z "${PROGRAM}" ] && {
echo "${initscript}: No ${NAME} program installed. Check: opkg list | grep ${NAME}"
exit 1
}
# sudo
config_get_bool value 'setup' 'sudo' '0'
[ "${value}" -ne 0 ] && PROGRAM="sudo ${PROGRAM}"
# interface
config_get value 'setup' 'interface'
[ -n "${value}" ] && append PROGRAM "-i ${value}"
# broadcast
config_get_bool value 'setup' 'broadcast' '0'
[ "${value}" -ne 0 ] && append PROGRAM '-b'
# wake up targets
config_foreach etherwake_start target $*
}
etherwake_start()
{
local section="$1"
shift
local names="$*"
local value=''
local target=''
if [ -z "${names}" ]
then
# check if boot target
config_get_bool value "${section}" 'wakeonboot' '0'
[ "${value}" -eq 0 ] && return 0
# wake up target
do_etherwake "${section}"
return $?
else
# name
config_get value "${section}" 'name'
[ -z "${value}" ] && return 0
for target in ${names}
do
[ "${value}" != "${target}" ] && continue
# wake up target
do_etherwake "${section}"
return $?
done
fi
}
# execute etherwake command for target
do_etherwake()
{
local section="$1"
local value=''
local password=''
local args=''
# password
config_get value "${section}" 'password'
[ -n "${value}" ] && {
password=$(etherwake_password "${value}")
append args "-p ${password}"
}
# mac address
config_get value "${section}" 'mac'
[ -z "${value}" ] && { echo "${initscript}: Target ${section} has no MAC address"; return 1; }
append args "${value}"
# name
config_get value "${section}" 'name'
[ -z "${value}" ] && value="{section}"
# execute command
echo "${initscript}: Waking up ${value} via ${PROGRAM}${args:+ ${args}}"
${PROGRAM} ${args}
return $?
}
# find first available program from searchlist
search_program()
{
local searchlist="$1"
local test=''
local program=''
for test in ${searchlist} ; do
[ -x "${test}" ] && {
program="${test}"
break;
}
done
[ -n "${program}" ] && echo "${program}"
return
}
# prepare hex password
etherwake_password()
{
local delimiter=':'
local password=`echo "$1" | sed "s/../&${delimiter}/g"`
echo "${password%${delimiter}}"
return
}
|