Support get multiple ipv4/ipv6 addresses in apf_utils
Test: atest NetworkStaticLibHostPythonTests Change-Id: I806696437acf905c049762d59a46c866ff0584ef
This commit is contained in:
parent
8a0613cc9e
commit
738bf999d5
1 changed files with 55 additions and 57 deletions
|
|
@ -83,14 +83,13 @@ def get_apf_counters_from_dumpsys(
|
|||
ad.log.debug("Getting apf counters: " + str(result))
|
||||
return result
|
||||
|
||||
def get_ipv4_address(
|
||||
def get_ipv4_addresses(
|
||||
ad: android_device.AndroidDevice, iface_name: str
|
||||
) -> str:
|
||||
"""Retrieves the IPv4 address of a given interface on an Android device.
|
||||
) -> list[str]:
|
||||
"""Retrieves the IPv4 addresses of a given interface on an Android device.
|
||||
|
||||
This function executes an ADB shell command (`ip -4 address show`) to get the
|
||||
network interface information and extracts the IPv4 address from the output.
|
||||
If devices has multiple IPv4 addresses, return the first one.
|
||||
If devices have no IPv4 address, raise PatternNotFoundException.
|
||||
|
||||
Args:
|
||||
|
|
@ -98,35 +97,31 @@ def get_ipv4_address(
|
|||
iface_name: The name of the network interface (e.g., "wlan0").
|
||||
|
||||
Returns:
|
||||
The IPv4 address of the interface as a string.
|
||||
|
||||
Raises:
|
||||
PatternNotFoundException: If the IPv4 address is not found in the command
|
||||
output.
|
||||
The IPv4 addresses of the interface as a list of string.
|
||||
Return empty list if no IPv4 address.
|
||||
"""
|
||||
# output format:
|
||||
# 54: wlan2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
||||
# inet 192.168.195.162/24 brd 192.168.195.255 scope global wlan2
|
||||
# valid_lft forever preferred_lft forever
|
||||
# inet 192.168.195.162/24 brd 192.168.195.255 scope global wlan2
|
||||
# valid_lft forever preferred_lft forever
|
||||
# inet 192.168.1.1/24 brd 192.168.1.255 scope global wlan2
|
||||
# valid_lft forever preferred_lft forever
|
||||
output = adb_utils.adb_shell(ad, f"ip -4 address show {iface_name}")
|
||||
pattern = r"inet\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/\d+"
|
||||
match = re.search(pattern, output)
|
||||
matches = re.findall(pattern, output)
|
||||
|
||||
if match:
|
||||
return match.group(1) # Extract the IPv4 address string.
|
||||
if matches:
|
||||
return matches
|
||||
else:
|
||||
raise PatternNotFoundException(
|
||||
"Cannot get hardware address for " + iface_name
|
||||
)
|
||||
return []
|
||||
|
||||
def get_ipv6_address(
|
||||
def get_ipv6_addresses(
|
||||
ad: android_device.AndroidDevice, iface_name: str
|
||||
) -> str:
|
||||
"""Retrieves the IPv6 address of a given interface on an Android device.
|
||||
) -> list[str]:
|
||||
"""Retrieves the IPv6 addresses of a given interface on an Android device.
|
||||
|
||||
This function executes an ADB shell command (`ip -6 address show`) to get the
|
||||
network interface information and extracts the IPv6 address from the output.
|
||||
If devices has multiple IPv6 addresses, return the first one.
|
||||
If devices have no IPv6 address, raise PatternNotFoundException.
|
||||
|
||||
Args:
|
||||
|
|
@ -134,29 +129,23 @@ def get_ipv6_address(
|
|||
iface_name: The name of the network interface (e.g., "wlan0").
|
||||
|
||||
Returns:
|
||||
The IPv6 address of the interface as a string.
|
||||
|
||||
Raises:
|
||||
PatternNotFoundException: If the IPv6 address is not found in the command
|
||||
output.
|
||||
The IPv6 addresses of the interface as a list of string.
|
||||
Return empty list if no IPv6 address.
|
||||
"""
|
||||
# output format
|
||||
# 54: wlan2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
|
||||
# inet6 fe80::10a3:5dff:fe52:de32/64 scope link
|
||||
# valid_lft forever preferred_lft forever
|
||||
# inet6 fe80::10a3:5dff:fe52:de32/64 scope link
|
||||
# valid_lft forever preferred_lft forever
|
||||
# inet6 fe80::1233:aadb:3d32:1234/64 scope link
|
||||
# valid_lft forever preferred_lft forever
|
||||
output = adb_utils.adb_shell(ad, f"ip -6 address show {iface_name}")
|
||||
if output is "":
|
||||
raise PatternNotFoundException(
|
||||
"Cannot get ipv6 address for " + iface_name
|
||||
)
|
||||
pattern = r"inet6\s+([0-9a-fA-F:]+)\/\d+"
|
||||
match = re.search(pattern, output)
|
||||
if match:
|
||||
return match.group(1) # Extract the IPv6 address string.
|
||||
matches = re.findall(pattern, output)
|
||||
|
||||
if matches:
|
||||
return matches
|
||||
else:
|
||||
raise PatternNotFoundException(
|
||||
"Cannot get IPv6 address for " + iface_name
|
||||
)
|
||||
return []
|
||||
|
||||
def get_hardware_address(
|
||||
ad: android_device.AndroidDevice, iface_name: str
|
||||
|
|
|
|||
Loading…
Reference in a new issue