一、代码实现
import subprocess import re import subprocess from common.logger import logger def get_device_wifi_address(udid): ip_command = f'adb -s {udid} shell ip route' result = subprocess.check_output(ip_command, shell=True, text=True) # 提取 IP 地址 ip_address = result.strip('\n').strip(' ').split(' ')[-1] # 使用正则表达式验证 IP 地址格式 ip_pattern = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') if ip_pattern.match(ip_address): logger.info(f"Device Wifi IP Address: {ip_address}") return ip_address else: raise Exception(f"Invalid IP address format: {ip_address}") device_udid = '9YS0220306003185' ip_address = get_device_wifi_address(device_udid) if ip_address: print(f"Device IP Address: {ip_address}") else: print("Unable to retrieve device IP address.")
二、Demo验证
输出结果:
Device IP Address: 192.168.2.103