File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2323import subprocess
2424import 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
3840def netldi_port (name ):
3941 result = subprocess .run (
@@ -71,7 +73,9 @@ def netldi_port(name):
7173 return entries [0 ]["Port" ]
7274
7375def 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
7781main ()
You can’t perform that action at this time.
0 commit comments