dpmkif3097 2016-04-02 11:15
浏览 254

从WMI PHP获取仅IPV4地址和排除IPv6

I Have Script to get IP Address based on PHP and WMI like this:

Get Active Adapter Network

function get_server_network_adapter() {

    $pc = "."; 
    $obj = new COM ("winmgmts:\\\\".$pc."\oot\\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;
    }
}

and Get IPAdress of Active Adapter

function get_server_ip() 
{
    $adapter = get_server_network_adapter();
    $pc = "."; 
    $obj = new COM ("winmgmts:\\\\".$pc."\oot\\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;
        }
    }
}

The Result of my code above is 192.168.1.103fe80::e9ad:9bb:e359:a122

However what I want is to only get the IPv4 like 192.168.1.103

Can someone help me? Thanks

  • 写回答

1条回答 默认 最新

  • dongye1912 2016-04-02 20:16
    关注

    Abstractedly from php tag, IPAddress property is simple an array in any used language. Read about it in MSDN article Win32_NetworkAdapterConfiguration class:

    IPAddress

    Data type: string array
    Access type: Read-only
    Qualifiers: MappingStrings
    ("Win32Registry|System\CurrentControlSet\Services|Parameters\Tcpip|IPAddress")

    Array of all of the IP addresses associated with the current network adapter. This property can contain either IPv6 addresses or IPv4 addresses. For more information, see IPv6 and IPv4 Support in WMI.

    However (above your question topic), identifying a network adapter via Name or Description property does not seem to be a good idea. Use InterfaceIndex property instead. Read Win32_NetworkAdapterConfiguration class as well as Win32_NetworkAdapter class MSDN articles:

    InterfaceIndex

    Data type: uint32
    Access type: Read-only

    Index value that uniquely identifies the local network interface. The value in this property is the same as the value in the InterfaceIndex property in the instance of Win32_IP4RouteTable that represents the network interface in the route table.

    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档