diff --git a/aioarping/__main__.py b/aioarping/__main__.py index 366628b..e9609aa 100644 --- a/aioarping/__main__.py +++ b/aioarping/__main__.py @@ -23,7 +23,10 @@ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE -import subprocess, asyncio, aioarping, ipaddress +import asyncio, aioarping, ipaddress + + +from .utils import get_interface_and_network def my_process(data): @@ -33,15 +36,7 @@ def my_process(data): event_loop = asyncio.get_event_loop() -mydomain = [ - x - for x in subprocess.getoutput( - "ip route|sed '/via/d' |sed '/docker/d'|sed '/linkdown/d'|sed '/src /!d' | sed '/dev /!d' |sed '2,$d'" - ).split(" ") - if x -] -myiface = mydomain[2] -mydomain = mydomain[0] +myiface, mydomain = get_interface_and_network() # First create and configure a raw socket mysocket = aioarping.create_raw_socket(myiface) diff --git a/aioarping/utils.py b/aioarping/utils.py new file mode 100644 index 0000000..46939e6 --- /dev/null +++ b/aioarping/utils.py @@ -0,0 +1,39 @@ +def prefilter_route_lines(lines): + """https://github.com/brandonmpace/routeparser/issues/2""" + + for el in lines: + if "linkdown" in el: + continue + if "src" not in el: + continue + yield el + + +def post_filter_routes(routes): + for r in routes: + if "docker" in r.interface: + continue + if r.gateway is not None: + continue + yield r + + +def get_ip_route_lines(): + import subprocess + + return subprocess.getoutput("/usr/sbin/ip route").splitlines() + + +def get_needed_routes(): + import routeparser + + routeLines = list(prefilter_route_lines(get_ip_route_lines())) + t = routeparser.RoutingTable.from_ip_route_lines(routeLines) + return post_filter_routes(t.routes) + + +def get_interface_and_network(): + r = next(iter(get_needed_routes())) + ifc = r.interface + domain = r.network + return ifc, domain diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..7452d64 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,28 @@ +[build-system] +requires = ["setuptools>=61.2.0", "wheel", "setuptools_scm[toml]>=3.4.3"] +build-backend = "setuptools.build_meta" + +[project] +name = "aioarping" +authors = [{name = "François Wautier", email = "francois@wautier.eu"}] +license = {text = "MIT"} +description = "API for arping over a LAN with asyncio." +readme = "README.md" +keywords = ["arp", "mac address", "presence", "automation"] +classifiers = [ + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.5", +] +urls = {Homepage = "http://github.com/frawau/aioarping"} +dependencies = ["ipaddress"] +dynamic = ["version"] + +[project.optional-dependencies] +utils = ["routeparser"] +cli = ["aioarping[utils]"] + +[tool.setuptools] +packages = ["aioarping"] + +[tool.setuptools_scm] diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 224a779..0000000 --- a/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[metadata] -description-file = README.md \ No newline at end of file diff --git a/setup.py b/setup.py deleted file mode 100644 index a616919..0000000 --- a/setup.py +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding:utf-8 -*- -from distutils.core import setup - -setup( - name="aioarping", - packages=["aioarping"], - version="0.1.3", - author="François Wautier", - author_email="francois@wautier.eu", - description="API for arping over a LAN with asyncio.", - url="http://github.com/frawau/aioarping", - download_url="http://github.com/frawau/aiolifx/archive/aioarping/0.1.3.tar.gz", - keywords=["arp", "mac address", "presence", "automation"], - license="MIT", - install_requires=["ipaddress",], - # See https://pypi.python.org/pypi?%3Aaction=list_classifiers - classifiers=[ - "License :: OSI Approved :: MIT License", - # Specify the Python versions you support here. In particular, ensure - # that you indicate whether you support Python 2, Python 3 or both. - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.5", - ], -)