From 80ed73b6fa3666c9a4d5c7b177e0848c903a1422 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Sat, 24 Nov 2018 15:45:12 +0100 Subject: [PATCH 1/2] Add filter and defaults to network scan --- googledevices/cli/commands.py | 26 ++++++++++++++++++++++---- setup.py | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/googledevices/cli/commands.py b/googledevices/cli/commands.py index 2655914..d617395 100644 --- a/googledevices/cli/commands.py +++ b/googledevices/cli/commands.py @@ -46,17 +46,35 @@ async def get_device_info(): @scan_network_group.command() -@click.argument('subnet', required=1) -def scan_network(subnet): +@click.option('--network', '-N', type=str, default=None, + help="The network you want to scan\ + in this format '192.168.1.0/24'.") +@click.option('--feature', '-F', type=str, default=None, + help="Filter discovery result to\ + units that contain these feature.") +def scan_network(network, feature): """Scan the entire subnet for Google devices.""" from googledevices.utils.scan import NetworkScan async def get_all_units(): - """Get device info from GH.""" + """Get device info for all Google devices.""" + all_devices = [] + if network is None: + import netifaces + gateway = netifaces.gateways() + subnet = gateway['default'][netifaces.AF_INET][0][:-1] + '0/24' + else: + subnet = network async with aiohttp.ClientSession() as session: googledevices = NetworkScan(LOOP, session) result = await googledevices.scan_for_units(subnet) - print(json.dumps(result, indent=4, sort_keys=True)) + if feature: + for unit in result: + if unit[feature]: + all_devices.append(unit) + else: + all_devices = result + print(json.dumps(all_devices, indent=4, sort_keys=True)) LOOP.run_until_complete(get_all_units()) diff --git a/setup.py b/setup.py index 371fd07..3ca6a43 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ author_email="ludeeus@gmail.com", description="", long_description=LONG, - install_requires=['aiohttp', 'async_timeout', 'click'], + install_requires=['aiohttp', 'async_timeout', 'click', 'netifaces'], long_description_content_type="text/markdown", url="https://github.com/ludeeus/googledevices", packages=setuptools.find_packages(), From ac140ed57910dd7b94992b206bc8b6a7f6c0b523 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Sat, 24 Nov 2018 15:51:31 +0100 Subject: [PATCH 2/2] linter :sunglasses: --- googledevices/cli/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googledevices/cli/commands.py b/googledevices/cli/commands.py index d617395..6be6c26 100644 --- a/googledevices/cli/commands.py +++ b/googledevices/cli/commands.py @@ -59,7 +59,7 @@ def scan_network(network, feature): async def get_all_units(): """Get device info for all Google devices.""" all_devices = [] - if network is None: + if network is None: import netifaces gateway = netifaces.gateways() subnet = gateway['default'][netifaces.AF_INET][0][:-1] + '0/24'