Last active
May 17, 2017 06:18
-
-
Save DawidvanGraan/7bf2e49c8f0d7b7012e7dcf84b6420ec to your computer and use it in GitHub Desktop.
This scripts scans for devices by logging into the TP-Link Archer D2 admin page and returning all devices connected to WiFi.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
__author__ = 'Dawid van Graan' | |
""" | |
TP-Link devices typically only allow one login at a time to the admin console. | |
This script will count towards your one allowed login. Depending on how | |
aggressively you configure this, you may not be able to access the | |
admin console of your TP-Link device without first stopping the script. | |
This scripts scans for devices by logging into the router admin page and returning all devices connected to WiFi. | |
Usage: tp-link-archer-d2.py -i YOUR_ROUTER_IP -p YOUR_ADMIN_PASSWORD | |
Example: tp-link-archer-d2.py -i 10.0.0.2 -p password | |
Works with: [TP-Link Archer D2](http://www.tp-link.co.za/products/details/cat-15_Archer-D2.html) Router. | |
""" | |
import base64 | |
import requests | |
import re | |
import configparser | |
import json | |
import sys, getopt | |
host = '' # host: YOUR_ROUTER_IP | |
password = '' # password: YOUR_ADMIN_PASSWORD | |
def scan_devices(host, password): | |
"""Scan for devices and return a list of found device""" | |
url = 'http://{}/cgi?5'.format(host) | |
credentials = '{}'.format(password).encode('utf') | |
# Encode the credentials to be sent as a cookie. | |
credentials = base64.b64encode(credentials).decode('utf') | |
# Create the authorization cookie. | |
cookie = 'Authorization=Basic {}'.format(credentials) | |
# Payload data | |
data = '[LAN_HOST_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]0,0\r\n'; | |
referer = 'http://{}'.format(host) | |
page = requests.post(url, data=data, headers={ | |
'cookie': cookie, | |
'referer': referer, | |
'origin': referer | |
}) | |
try: | |
config = configparser.ConfigParser(allow_no_value=True) | |
config.read_string(page.text) | |
results = [] | |
# Parse the conig file | |
for section in config.sections(): | |
if "IpAddress" in config[section]: | |
entry = {} | |
entry["ip"] = config[section]["IPAddress"] | |
entry["mac"] = config[section]["MACAddress"] | |
entry["name"] = config[section]["hostName"] | |
entry["active"] = config[section].getboolean("active") | |
results.append(entry) | |
return results | |
except ValueError: | |
# Error in response from Router | |
return None | |
# Read command line args | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "hi:p:", ["ip=","password="]) | |
except getopt.GetoptError: | |
print("Please see Usage: tp-link-archer-d2.py -ip YOUR_ROUTER_IP -password YOUR_ADMIN_PASSWORD") | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-help': | |
print("Usage: tp-link-archer-d2.py -i YOUR_ROUTER_IP -p YOUR_ADMIN_PASSWORD") | |
sys.exit() | |
elif opt in ("-i", "--ip"): | |
host = arg | |
elif opt in ("-p", "--password"): | |
password = arg | |
if host == '' or password == '': | |
print("Usage: tp-link-archer-d2.py -i YOUR_ROUTER_IP -p YOUR_ADMIN_PASSWORD") | |
sys.exit() | |
# Scan for Devices | |
results = scan_devices(host, password) | |
if results: | |
print json.dumps(results, indent=4) | |
else: | |
print "No Results Found" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment