一、代码实现
import subprocessimport re
import subprocessfrom common.logger import loggerdef 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_addresselse: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
欢迎技术交流: