-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtpmonitor.sh
118 lines (93 loc) · 3.17 KB
/
tpmonitor.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
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
## Want to switch your tplink smart plug on and off based on wether you PC (or device) is on? Save on power?
## this script will ping a network resource (a pc, phone, anything with an IP addresss) and turn on a Kasa Smart Plug
## on and off accordingly
## Adapted script from https://blog.georgovassilis.com/2016/05/07/controlling-the-tp-link-hs100-wi-fi-smart-plug/
## Notes for scheduling this script from Windows Task Scheduler:
##
## Works with Windows 10 with the bash shell installed.
## Taks Scheduler options:
##
## general | security options | run whether user is logged on or not
## triggers | at system startup
## actions | start a program | program script = bash | arguments = -c "/AddPathToScrip/tpmonitor.sh 1>/dev/null"
## settings | if the task fails, restart every 10 minutes
## --------------------------------------------------
## the payloads below are used to control the smart plug
# encoded {"system":{"set_relay_state":{"state":1}}}
payload_on="AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog=="
# encoded {"system":{"set_relay_state":{"state":0}}}
payload_off="AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu3qPeow=="
# encoded { "system":{ "get_sysinfo":null } }
payload_query="AAAAI9Dw0qHYq9+61/XPtJS20bTAn+yV5o/hh+jK8J7rh+vLtpbr"
## resource IP = static IP of device to be monitored
resource_ip=192.168.0.20
## plug IP = static IP of tp-link smart plug
plug_ip=192.168.0.80
## looptime = the polling time in seconts between checking the plug and device status
looptime=10
# flags
resource_status=off
plug_status=off
# decode utility for plug output
decode(){
echo "DECODE"
code=171
offset=4
input_num=`od -j $offset -An -t u1 -v | tr "\n" " "`
IFS=' ' read -r -a array <<< "$input_num"
args_for_printf=""
for element in "${array[@]}"
do
output=$(( $element ^ $code ))
args_for_printf="$args_for_printf\x$(printf %x $output)"
code=$element
done
printf "$args_for_printf"
}
# check if plug is on or off
check_plug_status(){
output=`echo -n $payload_query | base64 --decode | nc $plug_ip 9999 | decode | egrep -o 'relay_state":[0,1]' | egrep -o '[0,1]'`
if [[ $output -eq 1 ]]; then
plug_status=on
else
plug_status=off
fi
}
# check machine/device is on / pingable
check_resource_status(){
ping -W 5 -c 1 $resource_ip > /dev/null
if [[ $? -eq 0 ]]; then
resource_status=on
else
resource_status=off
fi
}
plug_on(){
echo "turn plug on"
echo -n "$payload_on" | base64 --decode | nc $plug_ip 9999 1>/dev/null || echo couldn''t connect to $ip:$port, nc failed with exit code $?
}
# turn plug off
plug_off(){
echo "turn plug off"
echo -n "$payload_off" | base64 --decode | nc $plug_ip 9999 1>/dev/null || echo couldn''t connect to $ip:$port, nc failed with exit code $?
}
# monitoring logic
monitor(){
check_resource_status
check_plug_status
echo "resource $resource_status"
echo "plug $plug_status"
echo "-----"
if [[ "$resource_status" = "on" && "$plug_status" = "off" ]]; then
plug_on
fi
if [[ "$resource_status" = "off" && "$plug_status" = "on" ]]; then
plug_off
fi
}
# main loop
while true
do
monitor
sleep $looptime
done