machine: improve LAN IP detection
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
8bbd269084
commit
c4e370fcff
|
@ -438,15 +438,23 @@ class Machine:
|
||||||
# However, it would be better to use NETLINK for this rather
|
# However, it would be better to use NETLINK for this rather
|
||||||
# than run another shell command
|
# than run another shell command
|
||||||
src_ip: Optional[str] = None
|
src_ip: Optional[str] = None
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
# First attempt: use "broadcast" to find the local IP
|
||||||
|
addr_info = [("<broadcast>", 0, False), ("10.255.255.255", 1, True)]
|
||||||
|
for (addr, port, bcast) in addr_info:
|
||||||
|
s = socket.socket(
|
||||||
|
socket.AF_INET, socket.SOCK_DGRAM | socket.SOCK_NONBLOCK
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
s.settimeout(0)
|
if bcast:
|
||||||
s.connect(('10.255.255.255', 1))
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
|
s.connect((addr, port))
|
||||||
src_ip = s.getsockname()[0]
|
src_ip = s.getsockname()[0]
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
continue
|
||||||
finally:
|
logging.info(f"Detected Local IP: {src_ip}")
|
||||||
s.close()
|
return src_ip
|
||||||
|
if src_ip is None:
|
||||||
|
logging.info("Failed to detect local IP address")
|
||||||
return src_ip
|
return src_ip
|
||||||
|
|
||||||
async def _get_wifi_interfaces(self) -> Dict[str, Any]:
|
async def _get_wifi_interfaces(self) -> Dict[str, Any]:
|
||||||
|
@ -456,7 +464,7 @@ class Machine:
|
||||||
try:
|
try:
|
||||||
resp = await self.iwgetid_cmd.run_with_response(log_complete=False)
|
resp = await self.iwgetid_cmd.run_with_response(log_complete=False)
|
||||||
except shell_cmd.error:
|
except shell_cmd.error:
|
||||||
logging.exception("Failed to run 'iwgetid' command")
|
logging.info("Failed to run 'iwgetid' command")
|
||||||
return {}
|
return {}
|
||||||
if resp:
|
if resp:
|
||||||
for line in resp.split("\n"):
|
for line in resp.split("\n"):
|
||||||
|
|
Loading…
Reference in New Issue