AlexandrErohin / TP-Link-Archer-C6U

Python package for API access and management for TP-Link Routers. See supported routers list
GNU General Public License v3.0
59 stars 22 forks source link

How to obtain 5Ghz network connected devices using this python api? #27

Open preintel opened 6 months ago

preintel commented 6 months ago

I am able to use the python script to get all the 2.4Ghz network connected devices list using the included script however I am not sure how I can also request the 5Ghz connected devices or if I need a separate script to run for them? I am running the script on an Archer AX73 TP-Link AX5400 Version 2.0 Firmware 1.1.0.


from tplinkrouterc6u import (
    TplinkRouterProvider,
    TplinkRouter,
    TplinkC1200Router,
    TPLinkMRClient,
    TPLinkDecoClient,
    Connection
)
from logging import Logger

router = TplinkRouterProvider.get_client('http://192.168.0.1', 'mypassword123here')
# You may use client directly like
# router = TplinkRouter('http://192.168.0.1', 'password')
# You may also pass username if it is different and a logger to log errors as
# router = TplinkRouter('http://192.168.0.1','password','admin2', Logger('test'))
# If you have the TP-link C1200 V2 or similar, you can use the TplinkC1200Router class instead of the TplinkRouter class.
# Remember that the password for this router is different, here you need to use the web encrypted password.
# To get web encrypted password, read Web Encrypted Password section
# router = TplinkC1200Router('http://192.168.0.1','WebEncryptedPassword', Logger('test'))

try:
    router.authorize()  # authorizing
    # Get firmware info - returns Firmware
    firmware = router.get_firmware()

    # Get status info - returns Status
    status = router.get_status()
   # if not status.guest_2g_enable:  # check if guest 2.4G wifi is disable
    # router.set_wifi(Connection.GUEST_2G, True)  # turn on guest 2.4G wifi

    # Get Address reservations, sort by ipaddr
    reservations = router.get_ipv4_reservations()
    reservations.sort(key=lambda a: a.ipaddr)
    for res in reservations:
        print(f"{res.macaddr} {res.ipaddr:16s} {res.hostname:36} {'Permanent':12}")

    # Get DHCP leases, sort by ipaddr
    leases = router.get_ipv4_dhcp_leases()
    leases.sort(key=lambda a: a.ipaddr)
    for lease in leases:
        print(f"{lease.macaddr} {lease.ipaddr:16s} {lease.hostname:36} {lease.lease_time:12}")
finally:
    router.logout()  # always logout as TP-Link Web Interface only supports upto 1 user logged

input("Enter any key to quit.") 
# sys.exit() is used to make the program quits. ( duh ) 
sys.exit() 
preintel commented 6 months ago

Also would like to get any devices labeled as "wired" connection in addition to 5Ghz, for some reason the TP-Link router switches devices between 2.4Ghz, 5Ghz and even wired (while they are only wireless connected) so ideally I want to get a list of ALL current connected devices to the router.

AlexandrErohin commented 6 months ago

@preintel You may use this code:

from tplinkrouterc6u import TplinkRouter, Connection
from logging import Logger

router = TplinkRouter('192.168.0.1', 'password', logger=Logger('test'))
router.authorize()
status = router.get_status()

for device in status.devices:
    if device.type in [Connection.HOST_5G, Connection.GUEST_5G, Connection.IOT_5G]:
        # do something with 5G connected device
    if device.type == Connection.WIRED:
        // do something with wired connected device

router.logout()
preintel commented 6 months ago

@AlexandrErohin thanks for the reply, I was able to get all the devices info with the below code However how can I get the leasetime by device as there is no device.lease_time property in the documentation. When I use the IPv4DHCPLease.lease_time it only shows the lease times of the devices connected to 2.4Ghz wifi only.

    for device in status.devices:
        print(f"{device.macaddr} {device.ipaddr:16s} {device.hostname:36}")