|
@@ -0,0 +1,132 @@
|
|
1
|
+#!/bin/sh /etc/rc.common
|
|
2
|
+# Copyright (C) 2009 OpenWrt.org
|
|
3
|
+
|
|
4
|
+NAME='etherwake'
|
|
5
|
+START=60
|
|
6
|
+PROGRAM=''
|
|
7
|
+
|
|
8
|
+start()
|
|
9
|
+{
|
|
10
|
+ local searchlist=''
|
|
11
|
+ local section=''
|
|
12
|
+ local value=''
|
|
13
|
+
|
|
14
|
+ config_load "${NAME}"
|
|
15
|
+
|
|
16
|
+ # check for available program
|
|
17
|
+ config_get searchlist 'setup' 'pathes'
|
|
18
|
+ PROGRAM=$(search_program "${searchlist}")
|
|
19
|
+ [ -z "${PROGRAM}" ] && {
|
|
20
|
+ echo "${initscript}: No ${NAME} program installed. Check: opkg list | grep ${NAME}"
|
|
21
|
+ exit 1
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ # sudo
|
|
25
|
+ config_get_bool value 'setup' 'sudo' '0'
|
|
26
|
+ [ "${value}" -ne 0 ] && PROGRAM="sudo ${PROGRAM}"
|
|
27
|
+
|
|
28
|
+ # interface
|
|
29
|
+ config_get value 'setup' 'interface'
|
|
30
|
+ [ -n "${value}" ] && append PROGRAM "-i ${value}"
|
|
31
|
+
|
|
32
|
+ # broadcast
|
|
33
|
+ config_get_bool value 'setup' 'broadcast' '0'
|
|
34
|
+ [ "${value}" -ne 0 ] && append PROGRAM '-b'
|
|
35
|
+
|
|
36
|
+ # wake up targets
|
|
37
|
+ config_foreach etherwake_start target $*
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+etherwake_start()
|
|
41
|
+{
|
|
42
|
+ local section="$1"
|
|
43
|
+ shift
|
|
44
|
+
|
|
45
|
+ local names="$*"
|
|
46
|
+
|
|
47
|
+ local value=''
|
|
48
|
+ local target=''
|
|
49
|
+
|
|
50
|
+ if [ -z "${names}" ]
|
|
51
|
+ then
|
|
52
|
+ # check if boot target
|
|
53
|
+ config_get_bool value "${section}" 'wakeonboot' '0'
|
|
54
|
+ [ "${value}" -eq 0 ] && return 0
|
|
55
|
+
|
|
56
|
+ # wake up target
|
|
57
|
+ do_etherwake "${section}"
|
|
58
|
+ return $?
|
|
59
|
+ else
|
|
60
|
+ # name
|
|
61
|
+ config_get value "${section}" 'name'
|
|
62
|
+ [ -z "${value}" ] && return 0
|
|
63
|
+
|
|
64
|
+ for target in ${names}
|
|
65
|
+ do
|
|
66
|
+ [ "${value}" != "${target}" ] && continue
|
|
67
|
+
|
|
68
|
+ # wake up target
|
|
69
|
+ do_etherwake "${section}"
|
|
70
|
+ return $?
|
|
71
|
+ done
|
|
72
|
+ fi
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+# execute etherwake command for target
|
|
76
|
+do_etherwake()
|
|
77
|
+{
|
|
78
|
+ local section="$1"
|
|
79
|
+ local value=''
|
|
80
|
+ local password=''
|
|
81
|
+ local args=''
|
|
82
|
+
|
|
83
|
+ # password
|
|
84
|
+ config_get value "${section}" 'password'
|
|
85
|
+ [ -n "${value}" ] && {
|
|
86
|
+ password=$(etherwake_password "${value}")
|
|
87
|
+ append args "-p ${password}"
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ # mac address
|
|
91
|
+ config_get value "${section}" 'mac'
|
|
92
|
+ [ -z "${value}" ] && { echo "${initscript}: Target ${section} has no MAC address"; return 1; }
|
|
93
|
+ append args "${value}"
|
|
94
|
+
|
|
95
|
+ # name
|
|
96
|
+ config_get value "${section}" 'name'
|
|
97
|
+ [ -z "${value}" ] && value="{section}"
|
|
98
|
+
|
|
99
|
+ # execute command
|
|
100
|
+ echo "${initscript}: Waking up ${value} via ${PROGRAM}${args:+ ${args}}"
|
|
101
|
+ ${PROGRAM} ${args}
|
|
102
|
+ return $?
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+# find first available program from searchlist
|
|
107
|
+search_program()
|
|
108
|
+{
|
|
109
|
+ local searchlist="$1"
|
|
110
|
+ local test=''
|
|
111
|
+ local program=''
|
|
112
|
+
|
|
113
|
+ for test in ${searchlist} ; do
|
|
114
|
+ [ -x "${test}" ] && {
|
|
115
|
+ program="${test}"
|
|
116
|
+ break;
|
|
117
|
+ }
|
|
118
|
+ done
|
|
119
|
+
|
|
120
|
+ [ -n "${program}" ] && echo "${program}"
|
|
121
|
+
|
|
122
|
+ return
|
|
123
|
+}
|
|
124
|
+
|
|
125
|
+# prepare hex password
|
|
126
|
+etherwake_password()
|
|
127
|
+{
|
|
128
|
+ local delimiter=':'
|
|
129
|
+ local password=`echo "$1" | sed "s/../&${delimiter}/g"`
|
|
130
|
+ echo "${password%${delimiter}}"
|
|
131
|
+ return
|
|
132
|
+}
|