Skip to content

Commit

Permalink
more ipv4s to display
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Jul 17, 2024
1 parent 6f24ff4 commit 0a8b1ab
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions pioreactor/utils/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def is_hostname_on_network(hostname: str, timeout: float = 10.0) -> bool:
return False


def is_reachable(hostname: str) -> bool:
def is_reachable(address: str) -> bool:
"""
Can we ping the computer at `hostname`?
Can we ping the computer at `address`?
"""
# TODO: why not use sh.ping? Ex: ping("leader7.local", "-c1", "-W50")
import subprocess

std_out_from_ping = subprocess.Popen(
["ping", "-c1", "-W50", hostname],
["ping", "-c1", "-W50", address],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
).stdout
Expand All @@ -62,22 +62,28 @@ def is_reachable(hostname: str) -> bool:


def get_ip() -> Optional[str]:
# returns ipv4
# returns all ipv4s as comma-separated string
from psutil import net_if_addrs

interfaces = ("wlan0", "eth0")
ipv4_addresses = []

interfaces = net_if_addrs()

for interface in interfaces:
if interface == "lo":
continue

for iface in interfaces:
try:
ipv4_addresses = [
addr.address for addr in net_if_addrs()[iface] if addr.family == 2
] # AddressFamily.AF_INET == 2
if ipv4_addresses:
return ipv4_addresses[0]
ipv4_addresses.extend(
[addr.address for addr in interfaces[interface] if addr.family == 2]
) # AddressFamily.AF_INET == 2
except Exception:
continue

return None
if ipv4_addresses:
return ",".join(ipv4_addresses)
else:
return None


def discover_workers_on_network(terminate: bool = False) -> Generator[str, None, None]:
Expand Down

0 comments on commit 0a8b1ab

Please sign in to comment.