From 8289401ddd3ef7da2ca52dbf6d3f56ee909136b9 Mon Sep 17 00:00:00 2001 From: Leif Neland Date: Wed, 18 Jul 2018 23:34:39 +0200 Subject: [PATCH 1/4] Repeat of djhworld Added verbose flag to print verbose output 04bd3b5 on Jun 24, 2017 --- tplink_smartplug.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index 4012a55..c1e8734 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -72,6 +72,7 @@ def decrypt(string): # Parse commandline arguments parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version)) parser.add_argument("-t", "--target", metavar="", required=True, help="Target hostname or IP address", type=validHostname) +parser.add_argument("-v", "--verbose", dest='verbose', action='store_true', help="Display verbose output") group = parser.add_mutually_exclusive_group(required=True) group.add_argument("-c", "--command", metavar="", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands) group.add_argument("-j", "--json", metavar="", help="Full JSON string of command to send") @@ -96,8 +97,14 @@ try: data = sock_tcp.recv(2048) sock_tcp.close() - print "Sent: ", cmd - print "Received: ", decrypt(data[4:]) + decrypted = decrypt(data[4:]) + + if args.verbose: + print "Sent: ", cmd + print "Received: ", decrypted + else: + print decrypted + except socket.error: quit("Cound not connect to host " + ip + ":" + str(port)) From da4835cb64c4eb2463dd91b0a240528cd6336f27 Mon Sep 17 00:00:00 2001 From: Thierry Laporte <37160474+rirethy@users.noreply.github.com> Date: Thu, 27 Sep 2018 11:36:07 +0200 Subject: [PATCH 2/4] Add "port" parameter Add management for a new parameter : "port". Allows to modify the defalt port, and not use the default one : 9999 This is useful to send a command to a plug that is not on the local network --- tplink_smartplug.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index 4012a55..131ce0f 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -33,6 +33,19 @@ def validHostname(hostname): parser.error("Invalid hostname.") return hostname +# Check if port is valid +def validPort(port): + try: + port = int(port) + except ValueError: + parser.error("Invalid port number.") + + if ((port <= 1024) or (port >65535)) : + parser.error("Invalid port number.") + + return port + + # Predefined Smart Plug Commands # For a full list of commands, consult tplink_commands.txt commands = {'info' : '{"system":{"get_sysinfo":{}}}', @@ -72,6 +85,7 @@ def decrypt(string): # Parse commandline arguments parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version)) parser.add_argument("-t", "--target", metavar="", required=True, help="Target hostname or IP address", type=validHostname) +parser.add_argument("-p", "--port", metavar="", default=9999, required=False, help="Target port", type=validPort) group = parser.add_mutually_exclusive_group(required=True) group.add_argument("-c", "--command", metavar="", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands) group.add_argument("-j", "--json", metavar="", help="Full JSON string of command to send") @@ -80,7 +94,7 @@ args = parser.parse_args() # Set target IP, port and command to send ip = args.target -port = 9999 +port = args.port if args.command is None: cmd = args.json else: From 0070bb27a16f44ba63f7c824b923ce685ee38fbd Mon Sep 17 00:00:00 2001 From: Leif Neland Date: Tue, 4 Dec 2018 09:01:18 +0100 Subject: [PATCH 3/4] Added -q for terse output, not command. Output is then only json, makes for easy parsing. --- tplink_smartplug.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index c1e8734..649877a 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -72,7 +72,7 @@ def decrypt(string): # Parse commandline arguments parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version)) parser.add_argument("-t", "--target", metavar="", required=True, help="Target hostname or IP address", type=validHostname) -parser.add_argument("-v", "--verbose", dest='verbose', action='store_true', help="Display verbose output") +parser.add_argument("-q", "--quiet", dest='quiet', action='store_true', help="Only show result") group = parser.add_mutually_exclusive_group(required=True) group.add_argument("-c", "--command", metavar="", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands) group.add_argument("-j", "--json", metavar="", help="Full JSON string of command to send") @@ -99,11 +99,11 @@ try: decrypted = decrypt(data[4:]) - if args.verbose: + if args.quiet: + print decrypted + else: print "Sent: ", cmd print "Received: ", decrypted - else: - print decrypted except socket.error: quit("Cound not connect to host " + ip + ":" + str(port)) From 20f12e043eb19592e916338ff9a72795d8de3bc2 Mon Sep 17 00:00:00 2001 From: softScheck GmbH Date: Thu, 7 May 2020 12:38:54 +0200 Subject: [PATCH 4/4] Added led commands --- tplink_smartplug.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index 984c889..ba0ecef 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -48,9 +48,11 @@ def validPort(port): # Predefined Smart Plug Commands # 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}}}', 'off' : '{"system":{"set_relay_state":{"state":0}}}', + 'ledoff' : '{"system":{"set_led_off":{"off":1}}}', + 'ledon' : '{"system":{"set_led_off":{"off":0}}}', 'cloudinfo': '{"cnCloud":{"get_info":{}}}', 'wlanscan' : '{"netif":{"get_scaninfo":{"refresh":0}}}', 'time' : '{"time":{"get_time":{}}}',