Skip to content

Commit 14ba975

Browse files
committed
test
1 parent 708714c commit 14ba975

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

client/bin/gs-info.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
import subprocess
2424
import sys
2525

26-
def netldi_ip():
26+
def local_outbound_ip():
2727
# Doesn't actually send any packets (UDP connect() just picks a route):
2828
# this reads back whichever local address the OS would use to reach the
2929
# outside world, i.e. this machine's own address as seen from beyond its
3030
# network stack -- the same thing `hostname -I` plus IPv4/IPv6 filtering
3131
# was after.
32-
probe_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
33-
probe_socket.connect(("8.8.8.8", 80))
34-
ip = probe_socket.getsockname()[0]
35-
probe_socket.close()
36-
return ip
32+
try:
33+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as probe_socket:
34+
probe_socket.connect(("8.8.8.8", 80))
35+
return probe_socket.getsockname()[0]
36+
except OSError as e:
37+
print(f"Could not determine this machine's outbound IP: {e}", file=sys.stderr)
38+
sys.exit(1)
3739

3840
def netldi_port(name):
3941
result = subprocess.run(
@@ -71,7 +73,9 @@ def netldi_port(name):
7173
return entries[0]["Port"]
7274

7375
def main():
76+
if len(sys.argv) != 2:
77+
sys.exit(f"Usage: {sys.argv[0]} <netldi-name>")
7478
name = sys.argv[1]
75-
print(netldi_ip(), netldi_port(name))
79+
print(local_outbound_ip(), netldi_port(name))
7680

7781
main()

0 commit comments

Comments
 (0)