mirror of
https://github.com/softScheck/tplink-smartplug
synced 2026-01-11 23:38:46 +01:00
Merge branch 'master' into master
This commit is contained in:
@@ -34,11 +34,26 @@ 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":{}}}',
|
||||
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":{}}}',
|
||||
@@ -95,6 +110,8 @@ else:
|
||||
# Parse commandline arguments
|
||||
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
|
||||
parser.add_argument("-t", "--target", metavar="<hostname>", required=True, help="Target hostname or IP address", type=validHostname)
|
||||
parser.add_argument("-p", "--port", metavar="<port>", default=9999, required=False, help="Target port", type=validPort)
|
||||
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="<command>", 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")
|
||||
@@ -103,7 +120,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:
|
||||
@@ -119,7 +136,13 @@ try:
|
||||
data = sock_tcp.recv(2048)
|
||||
sock_tcp.close()
|
||||
|
||||
print("Sent: ", cmd)
|
||||
print("Received: ", decrypt(data[4:]))
|
||||
decrypted = decrypt(data[4:])
|
||||
|
||||
if args.quiet:
|
||||
print decrypted
|
||||
else:
|
||||
print "Sent: ", cmd
|
||||
print "Received: ", decrypted
|
||||
|
||||
except socket.error:
|
||||
quit("Cound not connect to host " + ip + ":" + str(port))
|
||||
|
||||
Reference in New Issue
Block a user