Support arbitrary hostname instead of just IP

This commit is contained in:
David Haguenauer 2018-06-11 21:17:03 -04:00
parent 717db53032
commit 04a4b7cf81

View File

@ -25,13 +25,13 @@ import struct
version = 0.1 version = 0.1
# Check if IP is valid # Check if hostname is valid
def validIP(ip): def validHostname(hostname):
try: try:
socket.inet_pton(socket.AF_INET, ip) socket.gethostbyname(hostname)
except socket.error: except socket.error:
parser.error("Invalid IP Address.") parser.error("Invalid hostname.")
return ip return hostname
# 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
@ -70,7 +70,7 @@ def decrypt(string):
# 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 Client v" + str(version))
parser.add_argument("-t", "--target", metavar="<ip>", required=True, help="Target IP Address", type=validIP) parser.add_argument("-t", "--target", metavar="<hostname>", required=True, help="Target hostname or IP address", type=validHostname)
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>", 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") group.add_argument("-j", "--json", metavar="<JSON string>", help="Full JSON string of command to send")