| 1 |
"""Starting point for the AsusRouter.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import argparse |
| 6 |
import asyncio |
| 7 |
import logging |
| 8 |
|
| 9 |
import aiohttp |
| 10 |
|
| 11 |
from asusrouter import AsusRouter, AsusRouterDump, AsusRouterError |
| 12 |
from asusrouter.modules.data import AsusData |
| 13 |
|
| 14 |
_LOGGER = logging.getLogger(__name__) |
| 15 |
|
| 16 |
|
| 17 |
async def _connect_and_dump(args: argparse.Namespace) -> None: |
| 18 |
"""Connect to the router and dump the data.""" |
| 19 |
|
| 20 |
_LOGGER.info("Starting dump...") |
| 21 |
|
| 22 |
with AsusRouterDump(args.output, args.dump, args.zip) as dump: |
| 23 |
async with aiohttp.ClientSession() as session: |
| 24 |
_LOGGER.debug("Created aiohttp session") |
| 25 |
|
| 26 |
# Create the router |
| 27 |
router = AsusRouter( |
| 28 |
hostname=args.host, |
| 29 |
username=args.username, |
| 30 |
password=args.password, |
| 31 |
use_ssl=args.ssl, |
| 32 |
session=session, |
| 33 |
dumpback=dump.dump, |
| 34 |
) |
| 35 |
_LOGGER.debug("Created router") |
| 36 |
|
| 37 |
# Connect to the router and gather identity data |
| 38 |
_LOGGER.debug("Connecting to the router...") |
| 39 |
await router.async_connect() |
| 40 |
|
| 41 |
if not router.connected: |
| 42 |
_LOGGER.error("Failed to connect to the router") |
| 43 |
return |
| 44 |
|
| 45 |
_LOGGER.debug("Connected and identified") |
| 46 |
|
| 47 |
_LOGGER.debug("Checking all known data...") |
| 48 |
|
| 49 |
for datatype in AsusData: |
| 50 |
await router.async_get_data(datatype) |
| 51 |
|
| 52 |
_LOGGER.debug("Finished checking all known data") |
| 53 |
|
| 54 |
# Disconnect from the router |
| 55 |
await router.async_disconnect() |
| 56 |
_LOGGER.debug("Disconnected") |
| 57 |
|
| 58 |
_LOGGER.info("Dump finished. Saved to: %s", args.output) |
| 59 |
|
| 60 |
|
| 61 |
def main() -> None: |
| 62 |
"""Run AsusRouter as a program.""" |
| 63 |
|
| 64 |
parser = argparse.ArgumentParser( |
| 65 |
description="AsusRouter package command line interface." |
| 66 |
) |
| 67 |
parser.add_argument( |
| 68 |
"-o", |
| 69 |
"--output", |
| 70 |
type=str, |
| 71 |
required=True, |
| 72 |
help="The output folder for the log.", |
| 73 |
) |
| 74 |
parser.add_argument( |
| 75 |
"--dump", action="store_true", help="Perform a full dump." |
| 76 |
) |
| 77 |
parser.add_argument( |
| 78 |
"--host", |
| 79 |
type=str, |
| 80 |
required=True, |
| 81 |
help="The hostname / ip address of the router.", |
| 82 |
) |
| 83 |
parser.add_argument( |
| 84 |
"-u", |
| 85 |
"--username", |
| 86 |
type=str, |
| 87 |
default="admin", |
| 88 |
help="The username for the router. Default: `admin`.", |
| 89 |
) |
| 90 |
parser.add_argument( |
| 91 |
"-p", |
| 92 |
"--password", |
| 93 |
type=str, |
| 94 |
default="admin", |
| 95 |
help="The password for the router. Default: `admin`.", |
| 96 |
) |
| 97 |
parser.add_argument( |
| 98 |
"-s", |
| 99 |
"--ssl", |
| 100 |
action="store_true", |
| 101 |
default=False, |
| 102 |
help="Use SSL for the connection.", |
| 103 |
) |
| 104 |
parser.add_argument( |
| 105 |
"-P", |
| 106 |
"--port", |
| 107 |
type=int, |
| 108 |
default=None, |
| 109 |
help="The port to connect to. Only when using non-default settings.\ |
| 110 |
Default is 80 for HTTP and 443 for HTTPS.", |
| 111 |
) |
| 112 |
parser.add_argument( |
| 113 |
"--debug", action="store_true", help="Enable debug logging." |
| 114 |
) |
| 115 |
parser.add_argument( |
| 116 |
"--zip", action="store_true", default=False, help="Zip the output." |
| 117 |
) |
| 118 |
|
| 119 |
args = parser.parse_args() |
| 120 |
|
| 121 |
# Set up logging |
| 122 |
logging.basicConfig( |
| 123 |
level=logging.DEBUG if args.debug else logging.INFO, |
| 124 |
format="%(asctime)s [%(levelname)s] %(module)s: %(message)s", |
| 125 |
) |
| 126 |
|
| 127 |
# Connect to the router and dump the data |
| 128 |
try: |
| 129 |
asyncio.run(_connect_and_dump(args)) |
| 130 |
except AsusRouterError as ex: |
| 131 |
_LOGGER.error("AsusRouterError: %s", ex) |
| 132 |
|
| 133 |
|
| 134 |
if __name__ == "__main__": |
| 135 |
main() |