Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pulling original into fork #1

Merged
merged 4 commits into from
Oct 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions tplink_smartplug.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
#
# TP-Link Wi-Fi Smart Plug Protocol Client
# For use with TP-Link HS-100 or HS-110
Expand All @@ -24,7 +24,7 @@
import argparse
from struct import pack

version = 0.2
version = 0.3

# Check if hostname is valid
def validHostname(hostname):
Expand Down Expand Up @@ -112,6 +112,7 @@ def decrypt(string):
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")
parser.add_argument("--timeout", default=10, required=False, help="Timeout to establish connection")
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")
Expand All @@ -131,18 +132,20 @@ def decrypt(string):
# Send command and receive reply
try:
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_tcp.settimeout(int(args.timeout))
sock_tcp.connect((ip, port))
sock_tcp.settimeout(None)
sock_tcp.send(encrypt(cmd))
data = sock_tcp.recv(2048)
sock_tcp.close()

decrypted = decrypt(data[4:])

if args.quiet:
print decrypted
print(decrypted)
else:
print "Sent: ", cmd
print "Received: ", decrypted
print("Sent: ", cmd)
print("Received: ", decrypted)

except socket.error:
quit("Cound not connect to host " + ip + ":" + str(port))
quit("Could not connect to host " + ip + ":" + str(port))