我有脚本来获取基于 PHP 和 WMI 的 IP 地址,如下所示:
获取活动适配器网络
function get_server_network_adapter() {
$pc = ".";
$obj = new COM ("winmgmts:\\\\".$pc."\\root\\cimv2");
$wmi_network_rec = $obj->ExecQuery("Select * from Win32_NetworkAdapter Where NetConnectionStatus = 2");
foreach ($wmi_network_rec as $wmi_call) {
$adapter = $wmi_call->Name;
return $adapter;
}
}
并获取 Active Adapter 的 IPAaddress
function get_server_ip()
{
$adapter = get_server_network_adapter();
$pc = ".";
$obj = new COM ("winmgmts:\\\\".$pc."\\root\\cimv2");
$wmi_server_ip = $obj->ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where Description = '$adapter'");
foreach ($wmi_server_ip as $wmi_call) {
$ip = $wmi_call->IPAddress;
foreach ($ip as $key => $value) {
echo $value;
}
}
}
我上面代码的结果是192.168.1.103fe80::e9ad:9bb:e359:a122
但是我想要的是只获得像192.168.1.103这样的 IPv4
有人能帮我吗?谢谢