Pi.Alert icon indicating copy to clipboard operation
Pi.Alert copied to clipboard

[Feature Request] AsusRouter scan method

Open Th3D4rk0n3 opened this issue 1 year ago • 13 comments

Hello! Is it possible to add Asus router integration to Pi.Alert that would read the DHCP lease similar to Mikrotik and Unifi?

Th3D4rk0n3 avatar Sep 28 '23 19:09 Th3D4rk0n3

Since I don't have an ASUS router myself, I have to rely on pull requests for integration.

leiweibau avatar Sep 29 '23 04:09 leiweibau

I have an asus but dont know if I have the time... If running merlin:

just have to grab: /var/lib/misc/dnsmasq.leases

jeeftor avatar Jan 03 '24 22:01 jeeftor

Looks like there is a home assistant library I may be able to use to PR this in...

Locally I got the lib to log into the router and pull down the network table...

jeeftor avatar Jan 04 '24 03:01 jeeftor

I have an asus but dont know if I have the time... If running merlin:

just have to grab: /var/lib/misc/dnsmasq.leases

Well, if you use a different DNS this won't work.

Grabbing the device list from an http request (like the Home Assistant integration) will succeed instead!

If you want to test this feature I will be happy to help.

Thanks, Michele

mdima avatar Jan 07 '24 08:01 mdima

I actually looked at the home assistant library they are using which does basically a login and caches the cookie and then hits a cgi script to pull down the Network list.

I found the data and then didn't go any further than that...

import asyncio
import json

import aiohttp

from asusrouter import AsusRouter, Endpoint


async def main():

    async with aiohttp.ClientSession() as session:
        router = AsusRouter(
            hostname="192.168.1.1",      # required - both IP and URL supported
            username="admin",            # required
            password="MY_ROUTER_PASSWORD",      # required
            port=80,                   # optional - default port based on use_ssl
            use_ssl=False,               # optional
            cache_time=5 ,                # optional
            session=session
        )

        connection = await router.async_connect()
        id = await router.async_get_identity()

        result = await router.async_api_hook("get_clientlist()")
        fields = ['name','nickName','ip','mac','vendor','ipMethod']

        extracted_data = [{key: client[key] for key in fields if key in client} for client in
                          result['get_clientlist'].values()]

        print(json.dumps(extracted_data))


# Run the main function in an async context
asyncio.run(main())

I think somebody would have to parse this stuff out -> and drop it into SQL to make it work ...

jeeftor avatar Jan 07 '24 13:01 jeeftor

Thank you for your efforts so far. That looks good so far. This could certainly be helpful for a future pull request. Unfortunately, I still can't help here myself, as I still don't have an Asus router and don't want to roll out code without testing.

leiweibau avatar Jan 07 '24 15:01 leiweibau

That is a great result, just I do not have any experience in Python, I can try to integrate it but will take ages... :S

mdima avatar Jan 07 '24 16:01 mdima

Its a time issue for me ... I have work plus a bunch of other home assistant stuff I'm working on...

Really we just need to take the output of my function there and put it into the right data structure for SQL stuff... if somebody pings me in a few weeks maybe I'll bang it out

jeeftor avatar Jan 07 '24 17:01 jeeftor

I know this is a fork - but the codebase is ... well a MEGA file: https://github.com/leiweibau/Pi.Alert/blob/main/back/pialert.py

I'm worried if I was to make a PR I'd try to refactor the shit out of it and that would take even more of my time :)

jeeftor avatar Jan 07 '24 17:01 jeeftor

I know this is a fork - but the codebase is ... well a MEGA file:

Yes, I know. It grew over time and then I got used to it. I only started using Python with this project, bit by bit. In the next few days, I'm going to look into the "Sourcery" extension to "optimize" the code a bit. At the moment I don't have the knowledge to split the code into several files and what needs to be considered.

leiweibau avatar Jan 07 '24 18:01 leiweibau

For inspiration, this is how the code is split up in my fork: https://github.com/jokob-sk/Pi.Alert/tree/main/pialert - not sure how much of it can be re-used here.

jokob-sk avatar Jan 07 '24 21:01 jokob-sk

Could probably drop the whole thing into ChatGPT and have it refactor for you

jeeftor avatar Jan 07 '24 22:01 jeeftor

Could probably drop the whole thing into ChatGPT and have it refactor for you

Sourcery is also AI-based. I prefer to do this selectively for the time being so that nothing blows up. I've already managed to save 60 lines 👍

I tried to move a few functions to another script, but that doesn't really work yet.

leiweibau avatar Jan 08 '24 09:01 leiweibau