This code was made in Python to interact with the tp_link 840n router firmware
Firmware Version:0.9.1 4.16 v01e4.0 Build 180709 Rel.56645n.
This is a part of the code that I have already tested but when changing the password it gives me the aforementioned error
class TPLinkClient(object):
def init(self, username=‘’, password=‘’, router_url=“192.168.0.1”):
self.username = username
self.password = password
self.router_url = router_url
self.connection_string = ‘{}:{}’.format(self.username, self.password)
self.auth_token = ‘Authorization=Basic {}’.format(
base64.b64encode(self.connection_string.encode(‘ascii’)).decode(‘ascii’))
self.AUTH_HEADER = {
‘host’: self.router_url,
‘proxy-connection’: ‘keep-alive’,
‘content-type’: ‘text/plain, charset=utf-8’,
‘accept’: ‘/’,
‘origin’: ‘http://’ + self.router_url,
‘referer’: ‘http://’ + self.router_url + ‘/mainFrame.htm’,
‘accept-encoding’: ‘gzip, deflate’,
‘accept-language’: ‘en-GB,en-US;q=0.9,en;q=0.8’,
‘cookie’: self.auth_token,
‘sec-gpc’: ‘1’
}
self.NO_AUTH_HEADER = {
‘host’: self.router_url,
‘proxy-connection’: ‘keep-alive’,
‘content-type’: ‘text/plain’,
‘accept’: ‘/’,
‘origin’: ‘http://’ + self.router_url,
‘referer’: ‘http://’ + self.router_url + ‘/qr.htm’,
‘accept-encoding’: ‘gzip, deflate’,
‘accept-language’: ‘en-GB,en-US;q=0.9,en;q=0.8’,
‘sec-gpc’: ‘1’
}
def remove_mac_from_whitelist(self, mac_address):
“”"
Returns status of removal of MAC address from whitelisted
>>> random_mac = "02:00:00:%02x:%02x:%02x" % (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
>>> if "[error]0" in tp_client.whitelist_mac(random_mac, "temporary"):
remove_result = tp_client.remove_mac_from_whitelist(random_mac)
if "[error]0" in remove_result:
print("MAC removal is successful")
Output
-------
MAC removal is successful
:param mac_address: valid MAC address to be removed
:return: Python str "[error]0" means success
"""
url = "http://" + self.router_url + "/cgi?4"
listed_mac_addresses = self.get_whitelisted_mac_addresses()
id_of_the_mac = None
for each_mac_address in listed_mac_addresses:
if each_mac_address["MACAddress"].lower() == mac_address.lower():
id_of_the_mac = each_mac_address["id"][1:-1]
if id_of_the_mac:
payload = "[LAN_WLAN_MACTABLEENTRY#" + id_of_the_mac + "#0,0,0,0,0,0]0,0\r\n"
headers = self.AUTH_HEADER
response = requests.request("POST", url, headers=headers, data=payload).text
return response
else:
return "MAC address is not present in white list"
def get_wifi_connection_details(self):
“”"
Returns SSID and password of wifi connection
>>> print(tp_client.get_wifi_connection_details())
Output
-------
{'SSID': 'TP-Link_XXXXE', 'X_TP_PreSharedKey': '1234'}
:return: Python dict
"""
url = "http://" + self.router_url + "/cgi?5"
payload = "[LAN_WLAN#0,0,0,0,0,0#0,0,0,0,0,0]0," \
"16\r\nname\r\nSSID\r\nEnable\r\nX_TP_Configuration_Modified\r\nbeaconType\r\nStandard\r" \
"\nWEPEncryptionLevel\r\nWEPKeyIndex\r\nBasicEncryptionModes\r\nBasicAuthenticationMode\r" \
"\nWPAEncryptionModes\r\nWPAAuthenticationMode\r\nIEEE11iEncryptionModes\r" \
"\nIEEE11iAuthenticationMode\r\nX_TP_PreSharedKey\r\nX_TP_GroupKeyUpdateInterval\r\n "
headers = self.AUTH_HEADER
response = requests.request("POST", url, headers=headers, data=payload).text
ssid = re.findall(r"(?i)SSID=(.*)", response)[0]
x_tp_pre_shared_key = re.findall(r"(?i)X_TP_PreSharedKey=(\S+)", response)[0]
return {"SSID": ssid, "X_TP_PreSharedKey": x_tp_pre_shared_key}
def change_password(self, new_password):
“”"
Changes the network SSID and password.
User Authentication is required.
:param new_password: The new network password.
:return: Response from the router.
“”"
url = “http://” + self.router_url + “/cgi?2”
payload = “[LAN_WLAN#1,1,0,0,0,0#0,0,0,0,0,0]0,5\r\n”
“BeaconType=11i\r\n”
“IEEE11iAuthenticationMode=PSKAuthentication\r\n”
“IEEE11iEncryptionModes=AESEncryption\r\n”
“X_TP_PreSharedKey=” + new_password + “\r\n”
“X_TP_GroupKeyUpdateInterval=0”
headers = self.AUTH_HEADER
response = requests.request(“POST”, url, headers=headers, data=payload).text
print(response)
return response
}