Skip to content

Commit

Permalink
replace netifaces with ifaddr
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalzauberzeug committed May 13, 2024
1 parent db5214d commit 1eaaa36
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"--disable=W0511", // TODO
"--disable=W0718", // Catching too general exception
"--disable=W1514", // Using open without explicitly specifying an encoding
"--extension-pkg-allow-list=cv2,scipy.spatial,netifaces",
"--extension-pkg-allow-list=cv2,scipy.spatial",
"--generated-members=cv2.*,scipy.spatial.*,sh.*"
],
"python.testing.pytestArgs": ["tests"],
Expand Down
119 changes: 40 additions & 79 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ yappi = "^1.4"
pyloot = "^0.1.0"
objgraph = "^3.5.0"
imgsize = "^2.1"
netifaces = "^0.11.0"
ifaddr = "^0.2.0"
httpx = "^0.24.0"
matplotlib = "^3.7.2"
pyudev = ">=0.21.0"
Expand Down
21 changes: 10 additions & 11 deletions rosys/vision/rtsp_camera/arp_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
import sys
from typing import AsyncIterator, Optional

import netifaces
import ifaddr

from ... import rosys
from .vendors import VendorType, mac_to_vendor


def get_network_interface() -> str | None:
def get_network_adapter() -> str | None:
"""Return the first network interface that is not a loopback or virtual interface."""
for interface in netifaces.interfaces():
if any(interface.startswith(prefix) for prefix in ['lo', 'can', 'docker', 'veth', 'br']):
for adapter in ifaddr.get_adapters():
if any(adapter.name.startswith(prefix) for prefix in ['lo', 'can', 'docker', 'veth', 'br']):
continue
addresses = netifaces.ifaddresses(interface)
if netifaces.AF_INET not in addresses:
if adapter.ips is None:
continue
return interface
return adapter.name
return None


Expand All @@ -30,16 +29,16 @@ async def run_arp_scan() -> str:
arpscan_cmd = f'sudo {arpscan_cmd}'

cmd = f'{arpscan_cmd} --localnet'
interface = get_network_interface()
if interface is not None:
cmd += f' -I {interface}'
adapter = get_network_adapter()
if adapter is not None:
cmd += f' -I {adapter}'
output = await rosys.run.sh(cmd, timeout=10)

if 'sudo' in output and 'name resolution' not in output:
raise RuntimeError('Could not run arp-scan! Make sure it is installed and can be run with sudo.'
'Try running sudo visudo and add the following line: "rosys ALL=(ALL) NOPASSWD: /usr/sbin/arp-scan"')
if 'Could not obtain MAC address for interface' in output:
raise RuntimeError(f'arp-scan failed to obtain MAC address for interface {interface}')
raise RuntimeError(f'arp-scan failed to obtain MAC address for interface {adapter}')
return output


Expand Down

0 comments on commit 1eaaa36

Please sign in to comment.