-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawol.sh
executable file
·62 lines (54 loc) · 1.66 KB
/
awol.sh
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
#!/bin/bash
silent=$1
readarray hosts <<< `cat /home/pi/awol/Hostlist`
wakehost(){
for i in {1..10}; do sudo wakeonlan -i $1 $2 >/dev/null 2>&1; done
echoTime "Magic packages sent"
}
echoTime() {
if [[ "$silent" != *"--silent"* ]] || [[ "$2" == "1" ]]; then
echo "$(date)" - $1 >> /home/pi/awol/logs/awol.log
fi
}
echoLine() {
echoTime "--------------------------------------------------------------------"
}
echoLine
echoTime "AWOL Script is started"
echoLine
for i in "${!hosts[@]}"
do
readarray -d \| -t opt <<< "${hosts[$i]}"
readarray -d : -t meta <<< "${opt[0]}"
mac="${opt[1]}"
ping -c1 -w2 ${meta[1]} >/dev/null 2>&1 ;
ping_sts=$?
if [ $ping_sts -ne 0 ] ; then
on_batt=$(apcaccess -p STATUS | tr "\n" " ")
if [[ "$on_batt" == *"ONBAT"* ]]; then
if [[ "${meta[2]}" == *"always"* ]]; then
echoTime "$((i+1)). Host ${meta[0]} is down" 1
echoTime "${meta[0]} is configed to be Always on, waking it up" 1
wakehost ${meta[1]} ${mac}
else
echoTime " On Batteries... ignoring ${meta[0]}"
fi
else
if [[ "${meta[2]}" != *"skip"* ]]; then
echoTime "$((i+1)). Host ${meta[0]} is down" 1
echoTime "We are online, sending wake on lan magic packages to ${meta[0]}" 1
wakehost ${meta[1]} ${mac}
else
echoTime "$((i+1)). Host ${meta[0]} is down"
echoTime "${meta[0]} is configed to be skipped"
fi
fi
else
echoTime "$((i+1)). Host ${meta[0]} is up"
fi
echoLine
done
echoTime "Running Logrotate..."
logrotate /home/pi/awol/logrotate.conf --state /home/pi/awol/logrotate-state --verbose &> /dev/null
echoTime "AWOL Script is finished"
echoLine