@@ -79,19 +79,59 @@ def globals(**gs):
7979 print("SET {{}} = {{!r}}".format(g, gs[g]))
8080 multitest.flush()
8181 @staticmethod
82- def get_network_ip():
82+ def _get_ip_from_ifconfig(_nic, ipv6=False):
83+ # Helper to get IP address from an interface object using appropriate format
84+ addr_type = 'addr6' if ipv6 else 'addr4'
85+
86+ # First try newer format with addr type parameter
8387 try:
84- ip = nic.ifconfig()[0]
88+ ip = _nic.ifconfig(addr_type)
89+ if isinstance(ip, tuple) and len(ip) > 0:
90+ return ip[0]
91+ return ip
8592 except:
86- try:
87- import network
88- if hasattr(network, "WLAN"):
89- ip = network.WLAN().ifconfig()[0]
90- else:
91- ip = network.LAN().ifconfig()[0]
92- except:
93- ip = HOST_IP
94- return ip
93+ # Fallback to legacy format, but only for IPv4
94+ if not ipv6:
95+ try:
96+ return _nic.ifconfig()[0] # Legacy format
97+ except:
98+ pass
99+ return None
100+ @staticmethod
101+ def get_network_ip(ipv6=False):
102+ # Try with direct nic object if available
103+ try:
104+ if 'nic' in globals():
105+ ip = multitest._get_ip_from_ifconfig(nic, ipv6)
106+ if ip:
107+ return ip
108+ except:
109+ pass
110+
111+ # Find active network interface
112+ try:
113+ import network
114+ for attr_name in dir(network):
115+ if attr_name.startswith('__'):
116+ continue
117+ try:
118+ net_class = getattr(network, attr_name)
119+ if hasattr(net_class, 'active'):
120+ try:
121+ net_obj = net_class()
122+ if net_obj.active() and hasattr(net_obj, 'ifconfig'):
123+ ip = multitest._get_ip_from_ifconfig(net_obj, ipv6)
124+ if ip:
125+ return ip
126+ except:
127+ pass
128+ except:
129+ pass
130+ except:
131+ pass
132+
133+ # Fallback to host IP
134+ return HOST_IP6 if ipv6 else HOST_IP
95135 @staticmethod
96136 def expect_reboot(resume, delay_ms=0):
97137 print("WAIT_FOR_REBOOT", resume, delay_ms)
@@ -130,6 +170,24 @@ def get_host_ip(_ip_cache=[]):
130170 return _ip_cache [0 ]
131171
132172
173+ def get_host_ipv6 (_ipv6_cache = []):
174+ if not _ipv6_cache :
175+ try :
176+ import socket
177+
178+ # Try to find an IPv6 address by creating an IPv6 socket
179+ s = socket .socket (socket .AF_INET6 , socket .SOCK_DGRAM )
180+ # Connect to IPv6 Google DNS server to get our IPv6 address
181+ s .connect (("2001:4860:4860::8888" , 80 ))
182+ addr = s .getsockname ()[0 ]
183+ s .close ()
184+ _ipv6_cache .append (addr )
185+ except :
186+ # Fallback to localhost if unable to determine IPv6 address
187+ _ipv6_cache .append ("::1" )
188+ return _ipv6_cache [0 ]
189+
190+
133191class PyInstance :
134192 def __init__ (self ):
135193 pass
@@ -330,12 +388,19 @@ def run_test_on_instances(test_file, num_instances, instances):
330388 output = [[] for _ in range (num_instances )]
331389 output_metrics = []
332390
333- # If the test calls get_network_ip() then inject HOST_IP so that devices can know
334- # the IP address of the host. Do this lazily to not require a TCP/IP connection
335- # on the host if it's not needed.
391+ # If the test calls get_network_ip() or get_network_ipv6() then inject HOST_IP and HOST_IP6
392+ # so that devices can know the IP addresses of the host. Do this lazily to not require
393+ # a TCP/IP connection on the host if it's not needed.
336394 with open (test_file , "rb" ) as f :
337- if b"get_network_ip" in f .read ():
395+ file_content = f .read ()
396+ if b"get_network_ip" in file_content :
338397 injected_globals += "HOST_IP = '" + get_host_ip () + "'\n "
398+ # Also include IPv6 host IP if we can determine it
399+ try :
400+ host_ipv6 = get_host_ipv6 ()
401+ injected_globals += "HOST_IP6 = '" + host_ipv6 + "'\n "
402+ except :
403+ injected_globals += "HOST_IP6 = '::1'\n " # Default to localhost
339404
340405 if cmd_args .trace_output :
341406 print ("TRACE {}:" .format ("|" .join (str (i ) for i in instances )))
0 commit comments