-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtplink_hs110_cmd.pl
executable file
·301 lines (281 loc) · 9.19 KB
/
tplink_hs110_cmd.pl
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/perl
# Command Line Tool for TP-Link HS-100/110 wifi controlled power outlets
# Copyright: Volker Kettenbach, 2016
#
# This implements many commands but not all.
# The program is focused on querying most of the data the HS100/110 does provide.
# Tough it can only turn it on and off and enable/disabled the nightmode.
# You can't set things like schedule, wifi network etc.
# Use the (not so bad at all) smartphone app for this.
# If you want to implement more commands, see tplink-smarthome-commands.txt for a full list
# and submit your changes as a pull request to my github repository.
use strict;
use warnings;
use IO::Socket::INET;
use IO::Socket::Timeout;
use JSON;
use Getopt::Simple;
use Data::Dumper;
my $timeout=3;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon++;
$year += 1900;
my %commands = ( 'info' => '{"system":{"get_sysinfo":{}}}',
'on' => '{"system":{"set_relay_state":{"state":1}}}',
'off' => '{"system":{"set_relay_state":{"state":0}}}',
'cloudinfo' => '{"cnCloud":{"get_info":{}}}',
'wlanscan' => '{"netif":{"get_scaninfo":{"refresh":0}}}',
'wlanscanfresh' => '{"netif":{"get_scaninfo":{"refresh":1}}}',
'json' => '', #to be set later
'time' => '{"time":{"get_time":{}}}',
'schedule' => '{"schedule":{"get_rules":{}}}',
'countdown' => '{"count_down":{"get_rules":{}}}',
'antitheft' => '{"anti_theft":{"get_rules":{}}}',
'nightmodeon' => '{"system":{"set_led_off":{"off":1}}}',
'nightmodeoff' => '{"system":{"set_led_off":{"off":0}}}',
'realtime' => '{"emeter":{"get_realtime":{}}}',
'monthstat' => '{"emeter":{"get_monthstat":{"year":'.$year.'}}}',
'daystat' => '{"emeter":{"get_daystat":{"month":'.$mon.',"year":'.$year.'}}}',
'reboot' => '{"system":{"reboot":{"delay":1}}}'
);
my $remote_port = 9999;
my $clist;
foreach(sort keys %commands) {
$clist .= $_ .", ";
}
my ($options) = { help => {
type => '',
default => '',
order => 1,
verbose => 'Print this help text'
},
ip => {
type => '=s',
verbose => 'Specifiy the hostname/ip of the TPLink HS100/110'
},
command => {
type => '=s',
verbose => 'Specify the command to send to the TPLink HS100/110 out of: '.$clist
},
verbose => {
verbose => 'Be verbose'
},
json => {
type => '=s',
verbose => 'arbitrary json string to send to the TPLink HS100/110 (check tplink-smarthome-commands.txt)'
}
};
my($option) = Getopt::Simple -> new();
if (!$option -> getOptions($options, "Usage: $0 [options]") ) {
exit(-1); # Failure.
}
my $isVerbose=0;
if ($$option{'switch'}{'verbose'}) {
$isVerbose=1;
}
my $command;
my $jcommand = $commands{$$option{'switch'}{'command'}};
if (!$$option{'switch'}{'command'}) {
$option->helpOptions();
print "No command given!";
exit (-1);
} else {
if (!exists($commands{$$option{'switch'}{'command'}})){
print "Invalid command! ";
print "Please give a command out of: $clist";
exit (-1);
} else {
if ($isVerbose){
print "Sending command: <".$$option{'switch'}{'command'}."> = $jcommand";
}
}
$command = $$option{'switch'}{'command'};
}
my $remote_host;
if (!$$option{'switch'}{'ip'}) {
$option->helpOptions();
print "No ip given!";
exit (-1);
} else {
if ($isVerbose){
#print " to " . $$option{'switch'}{'ip'}.". ($jcommand) \n";
print " to " . $$option{'switch'}{'ip'}.". \n";
}
$remote_host = $$option{'switch'}{'ip'};
}
if($command eq 'json') {
$jcommand = $$option{'switch'}{'json'};
}
# Encryption and Decryption of TP-Link Smart Home Protocol
# XOR Autokey Cipher with starting key = 171
# Based on https://www.softscheck.com/en/reverse-engineering-tp-link-hs110/
sub encrypt {
my $key = 171;
my @string=split(//, $_[0]);
my $result = "\0\0\0".chr(@string);
foreach (@string) {
my $a = $key ^ ord($_);
$key = $a;
$result .= chr($a);
}
return $result;
}
sub decrypt {
my $key = 171;
my $result = "";
my @string=split(//, $_[0]);
foreach (@string) {
my $a = $key ^ ord($_);
$key = ord($_);
$result .= chr($a);
}
return $result;
}
my $c = encrypt($jcommand);
my $socket = IO::Socket::INET->new(PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto => 'tcp',
Type => SOCK_STREAM,
Timeout => $timeout)
or die "Couldn't connect to $remote_host:$remote_port: $@\n";
$socket->write($c);
IO::Socket::Timeout->enable_timeouts_on($socket);
$socket->read_timeout(.5);
my $data;
$data = <$socket>;
$socket->close();
$data = decrypt(substr($data,4));
print "Received answer: " . $data. "\n" if $isVerbose;
my $json = decode_json($data);
if ($command eq "on" || $command eq "off") {
if ($json->{'system'}->{'set_relay_state'}->{'err_code'} eq "0") {
print "Command successfull" if $isVerbose;
exit (0);
} else{
print "Command failed";
exit(1);
}
}
if ($command eq "reboot"){
if ($json->{'system'}->{'reboot'}->{'err_code'} eq "0") {
print "Command successfull" if $isVerbose;
exit (0);
} else{
print "Command failed";
exit(1);
}
}
if ($command eq "time") {
print "The time on $remote_host is: ";
print $json->{'time'}->{'get_time'}->{'year'}."-";
print $json->{'time'}->{'get_time'}->{'month'}."-";
print $json->{'time'}->{'get_time'}->{'mday'}. " ";
print $json->{'time'}->{'get_time'}->{'hour'}.":";
print $json->{'time'}->{'get_time'}->{'min'}.":";
print $json->{'time'}->{'get_time'}->{'sec'};
}
if ($command eq "info"){
print "Results: \n";
foreach my $key (sort keys %{$json->{'system'}->{'get_sysinfo'}}) {
print " " . $key . ": " . $json->{'system'}->{'get_sysinfo'}->{$key} . "\n";
}
}
if ($command eq "cloudinfo"){
print "Results: \n";
foreach my $key (sort keys %{$json->{'cnCloud'}->{'get_info'}}) {
print " " . $key . ": " . $json->{'cnCloud'}->{'get_info'}->{$key} . "\n";
}
}
if ($command eq "realtime"){
print "Results: \n";
foreach my $key (sort keys %{$json->{'emeter'}->{'get_realtime'}}) {
print " " . $key . ": " . $json->{'emeter'}->{'get_realtime'}->{$key} . "\n";
}
}
if ($command eq "monthstat"){
print "Results: \n";
foreach my $key (sort keys @{$json->{'emeter'}->{'get_monthstat'}->{'month_list'}}) {
foreach my $key2 ($json->{'emeter'}->{'get_monthstat'}->{'month_list'}[$key]) {
print $key2->{'year'}."-".$key2->{'month'}.": " . $key2->{'energy'}."\n";
}
}
}
if ($command eq "daystat"){
my $total=0;
print "Results: \n";
foreach my $key (sort keys @{$json->{'emeter'}->{'get_daystat'}->{'day_list'}}) {
foreach my $key2 ($json->{'emeter'}->{'get_daystat'}->{'day_list'}[$key]) {
print $key2->{'year'}."-".$key2->{'month'}."-".$key2->{'day'}.": " . $key2->{'energy'}."\n";
$total = $total+ $key2->{'energy'};
}
}
my $count = @{$json->{'emeter'}->{'get_daystat'}->{'day_list'}};
print "Monthly total: $total\n";
print "Daily average: " . $total / $count;
}
if ($command eq "antitheft"){
if (scalar (@{$json->{'anti_theft'}->{'get_rules'}->{'rule_list'}}) eq 0) {
print "No awaymode set";
} else {
print "Aaway Mode: \n";
foreach my $key (sort keys @{$json->{'anti_theft'}->{'get_rules'}->{'rule_list'}}) {
foreach my $key2 ($json->{'anti_theft'}->{'get_rules'}->{'rule_list'}[$key]) {
print "Enabled: " if ($key2->{'enable'} eq 1);
print "Disabled: " if ($key2->{'enable'} eq 0);
print Dumper $key2;
}
}
}
}
if ($command eq "countdown"){
if (scalar (@{$json->{'count_down'}->{'get_rules'}->{'rule_list'}}) eq 0) {
print "No timer set";
} else {
print "Timer: \n";
foreach my $key (sort keys @{$json->{'count_down'}->{'get_rules'}->{'rule_list'}}) {
foreach my $key2 ($json->{'count_down'}->{'get_rules'}->{'rule_list'}[$key]) {
print "Timer enabled: " if ($key2->{'enable'} eq 1);
print "Timer disabled: " if ($key2->{'enable'} eq 0);
print $key2->{'name'} . " ";
print "Delay: " . $key2->{'delay'}."s, ";
print "Activity: ";
print "On" if ($key2->{'act'} eq 1);
print "Off" if ($key2->{'act'} eq 0);
}
}
}
}
if ($command eq "schedule"){
if (scalar (@{$json->{'schedule'}->{'get_rules'}->{'rule_list'}}) eq 0) {
print "No schedules";
} else {
print "Schedules:\n";
foreach my $key (sort keys @{$json->{'schedule'}->{'get_rules'}->{'rule_list'}}) {
foreach my $key2 ($json->{'schedule'}->{'get_rules'}->{'rule_list'}[$key]) {
print "Enabled rule: " if ($key2->{'enable'} eq 1);
print "Disabled rule: " if ($key2->{'enable'} eq 0);
print $key2->{'name'} . " ";
print Dumper $key2;
}
}
}
}
if ($command eq "wlanscan" | $command eq "wlanscanfresh"){
if (scalar (@{$json->{'netif'}->{'get_scaninfo'}->{'ap_list'}}) eq 0) {
print "No networks. Try command <wlanscanfresh>";
} else {
print "Networks:\n";
foreach my $key (sort keys @{$json->{'netif'}->{'get_scaninfo'}->{'ap_list'}}) {
foreach my $key2 ($json->{'netif'}->{'get_scaninfo'}->{'ap_list'}[$key]) {
print "\t";
print "Open" if ($key2->{'key_type'} eq 0);
print "WEP" if ($key2->{'key_type'} eq 1);
print "WPA" if ($key2->{'key_type'} eq 2);
print "WPA2" if ($key2->{'key_type'} eq 3);
print "\t";
print $key2->{'ssid'} . "\n";
}
}
}
}