tplink-smartplug/tplink_smartplug.py

103 lines
3.1 KiB
Python
Raw Permalink Normal View History

2018-07-04 12:23:05 +02:00
#!/usr/bin/env python2
#
2016-07-28 13:52:52 +02:00
# TP-Link Wi-Fi Smart Plug Protocol Client
# For use with TP-Link HS-100 or HS-110
2018-07-04 12:23:05 +02:00
#
2016-07-28 13:52:52 +02:00
# by Lubomir Stroetmann
2018-07-04 12:23:05 +02:00
# Copyright 2016 softScheck GmbH
#
2016-07-28 13:52:52 +02:00
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
2018-07-04 12:23:05 +02:00
#
2016-07-28 13:52:52 +02:00
# http://www.apache.org/licenses/LICENSE-2.0
2018-07-04 12:23:05 +02:00
#
2016-07-28 13:52:52 +02:00
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
2018-07-04 12:23:05 +02:00
2016-07-28 13:52:52 +02:00
import socket
import argparse
2017-11-08 11:16:45 +01:00
from struct import pack
2016-07-28 13:52:52 +02:00
2018-07-04 12:23:05 +02:00
version = 0.2
2016-07-28 13:52:52 +02:00
2018-07-04 12:23:05 +02:00
# Check if hostname is valid
def validHostname(hostname):
2016-07-28 13:52:52 +02:00
try:
2018-07-04 12:23:05 +02:00
socket.gethostbyname(hostname)
2016-07-28 13:52:52 +02:00
except socket.error:
2018-07-04 12:23:05 +02:00
parser.error("Invalid hostname.")
return hostname
2016-07-28 13:52:52 +02:00
# Predefined Smart Plug Commands
# For a full list of commands, consult tplink_commands.txt
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}}}',
'time' : '{"time":{"get_time":{}}}',
'schedule' : '{"schedule":{"get_rules":{}}}',
'countdown': '{"count_down":{"get_rules":{}}}',
'antitheft': '{"anti_theft":{"get_rules":{}}}',
'reboot' : '{"system":{"reboot":{"delay":1}}}',
'reset' : '{"system":{"reset":{"delay":1}}}',
'energy' : '{"emeter":{"get_realtime":{}}}'
2016-07-28 13:52:52 +02:00
}
# Encryption and Decryption of TP-Link Smart Home Protocol
# XOR Autokey Cipher with starting key = 171
def encrypt(string):
key = 171
2017-11-08 11:16:45 +01:00
result = pack('>I', len(string))
2018-07-04 12:23:05 +02:00
for i in string:
2016-07-28 13:52:52 +02:00
a = key ^ ord(i)
key = a
result += chr(a)
return result
def decrypt(string):
2018-07-04 12:23:05 +02:00
key = 171
2016-07-28 13:52:52 +02:00
result = ""
2018-07-04 12:23:05 +02:00
for i in string:
2016-07-28 13:52:52 +02:00
a = key ^ ord(i)
2018-07-04 12:23:05 +02:00
key = ord(i)
2016-07-28 13:52:52 +02:00
result += chr(a)
return result
# Parse commandline arguments
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
2018-07-04 12:23:05 +02:00
parser.add_argument("-t", "--target", metavar="<hostname>", required=True, help="Target hostname or IP address", type=validHostname)
2016-07-28 13:52:52 +02:00
group = parser.add_mutually_exclusive_group(required=True)
2018-07-04 12:23:05 +02:00
group.add_argument("-c", "--command", metavar="<command>", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands)
2016-07-28 13:52:52 +02:00
group.add_argument("-j", "--json", metavar="<JSON string>", help="Full JSON string of command to send")
args = parser.parse_args()
2018-07-04 12:23:05 +02:00
2016-07-28 13:52:52 +02:00
# Set target IP, port and command to send
ip = args.target
port = 9999
if args.command is None:
cmd = args.json
else:
cmd = commands[args.command]
2018-07-04 12:23:05 +02:00
# Send command and receive reply
2016-07-28 13:52:52 +02:00
try:
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_tcp.connect((ip, port))
sock_tcp.send(encrypt(cmd))
data = sock_tcp.recv(2048)
sock_tcp.close()
2018-07-04 12:23:05 +02:00
2018-08-06 19:15:13 +02:00
print decrypt(data[4:])
2016-07-28 13:52:52 +02:00
except socket.error:
quit("Cound not connect to host " + ip + ":" + str(port))
2018-07-04 12:23:05 +02:00