Changed to python3 compatability and PEP8 style guidelines.

This commit is contained in:
Martin West 2017-09-25 09:57:03 +02:00
parent a8f9e9d65c
commit ec62f6f869
1 changed files with 73 additions and 57 deletions

View File

@ -22,10 +22,11 @@
import socket import socket
import argparse import argparse
version = 0.1 version = 0.2
# Check if IP is valid
def validIP(ip): def validIP(ip):
"""Check if IP is valid"""
try: try:
socket.inet_pton(socket.AF_INET, ip) socket.inet_pton(socket.AF_INET, ip)
except socket.error: except socket.error:
@ -34,45 +35,61 @@ def validIP(ip):
# Predefined Smart Plug Commands # Predefined Smart Plug Commands
# For a full list of commands, consult tplink_commands.txt # For a full list of commands, consult tplink_commands.txt
commands = {'info' : '{"system":{"get_sysinfo":{}}}', commands = {'info': '{"system":{"get_sysinfo":{}}}',
'on' : '{"system":{"set_relay_state":{"state":1}}}', 'on': '{"system":{"set_relay_state":{"state":1}}}',
'off' : '{"system":{"set_relay_state":{"state":0}}}', 'off': '{"system":{"set_relay_state":{"state":0}}}',
'cloudinfo': '{"cnCloud":{"get_info":{}}}', 'cloudinfo': '{"cnCloud":{"get_info":{}}}',
'wlanscan' : '{"netif":{"get_scaninfo":{"refresh":0}}}', 'wlanscan': '{"netif":{"get_scaninfo":{"refresh":0}}}',
'time' : '{"time":{"get_time":{}}}', 'time': '{"time":{"get_time":{}}}',
'schedule' : '{"schedule":{"get_rules":{}}}', 'schedule': '{"schedule":{"get_rules":{}}}',
'countdown': '{"count_down":{"get_rules":{}}}', 'countdown': '{"count_down":{"get_rules":{}}}',
'antitheft': '{"anti_theft":{"get_rules":{}}}', 'antitheft': '{"anti_theft":{"get_rules":{}}}',
'reboot' : '{"system":{"reboot":{"delay":1}}}', 'reboot': '{"system":{"reboot":{"delay":1}}}',
'reset' : '{"system":{"reset":{"delay":1}}}' 'reset': '{"system":{"reset":{"delay":1}}}'
} }
# Encryption and Decryption of TP-Link Smart Home Protocol # Encryption and Decryption of TP-Link Smart Home Protocol
# XOR Autokey Cipher with starting key = 171 # XOR Autokey Cipher with starting key = 171
# def encrypt(string):
# key = 171
# result = "\0\0\0\0"
# for i in string:
# a = key ^ ord(i)
# key = a
# result += chr(a)
# return result
def encrypt(string): def encrypt(string):
key = 171 key = 171
result = "\0\0\0\0" result = b"\0\0\0" + bytes([len(string)])
for i in string: for i in string.encode('latin-1'):
a = key ^ ord(i) a = key ^ i
key = a key = a
result += chr(a) result += bytes([a])
return result return result
def decrypt(string): def decrypt(string):
key = 171 key = 171
result = "" result = b""
for i in string: for i in string:
a = key ^ ord(i) a = key ^ i
key = ord(i) key = i
result += chr(a) result += bytes([a])
return result return result.decode('latin-1')
# Parse commandline arguments # Parse commandline arguments
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version)) parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug " +
parser.add_argument("-t", "--target", metavar="<ip>", required=True, help="Target IP Address", type=validIP) "Client v" + str(version))
parser.add_argument("-t", "--target", metavar="<ip>", required=True,
help="Target IP Address", type=validIP)
group = parser.add_mutually_exclusive_group(required=True) group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-c", "--command", metavar="<command>", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands) group.add_argument("-c", "--command", metavar="<command>",
group.add_argument("-j", "--json", metavar="<JSON string>", help="Full JSON string of command to send") help="Preset command to send. Choices are: " +
", ".join(commands), choices=commands)
group.add_argument("-j", "--json", metavar="<JSON string>",
help="Full JSON string of command to send")
args = parser.parse_args() args = parser.parse_args()
# Set target IP, port and command to send # Set target IP, port and command to send
@ -83,9 +100,8 @@ if args.command is None:
else: else:
cmd = commands[args.command] cmd = commands[args.command]
# Send command and receive reply # Send command and receive reply
try: try:
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_tcp.connect((ip, port)) sock_tcp.connect((ip, port))
@ -93,7 +109,7 @@ try:
data = sock_tcp.recv(2048) data = sock_tcp.recv(2048)
sock_tcp.close() sock_tcp.close()
print "Sent: ", cmd print("Sent: ", cmd)
print "Received: ", decrypt(data[4:]) print("Received: ", decrypt(data[4:]))
except socket.error: except socket.error:
quit("Cound not connect to host " + ip + ":" + str(port)) quit("Cound not connect to host " + ip + ":" + str(port))