Get ip address

这段代码展示了如何在C++和Java中获取本地主机的网络接口地址。C++版本使用`getifaddrs`函数遍历并打印IPv4和IPv6地址,而Java版本通过`NetworkInterface`枚举所有接口,过滤掉回环地址,只显示非回环的Inet4Address。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <iostream>
#include <ifaddrs.h>
#include <string.h>

using namespace std;

int main()
{
    struct ifaddrs * ifAddrStruct=NULL;
    struct ifaddrs * ifa=NULL;
    void * tmpAddrPtr=NULL;

    getifaddrs(&ifAddrStruct);

    for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
        if (!ifa->ifa_addr) {
            continue;
        }
		
		if ((strcmp("lo", ifa->ifa_name) == 0) || !(ifa->ifa_flags & (IFF_RUNNING))) {
				continue;
		}
        if (ifa->ifa_addr->sa_family == AF_INET) { 
            tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
            char addressBuffer[INET_ADDRSTRLEN];
            inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
			std::cout << std::string(addressBuffer) << std::endl;

        } else if (ifa->ifa_addr->sa_family == AF_INET6) {
            tmpAddrPtr= &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
            char addressBuffer[INET6_ADDRSTRLEN];
            inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
			std::cout << std::string(addressBuffer) << std::endl;
        } 
    }
    if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);

    return 0;
}

For Java:

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

private String getAddress() {
    try {
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
            Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress ip = (InetAddress) addresses.nextElement();
                if (ip instanceof Inet4Address
                        && !ip.isLoopbackAddress()
                        && !ip.getHostAddress().contains(":")) {
                    String ipHostAddress = ip.getHostAddress();
                    return ipHostAddress;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

<< `wmic path Win32_NetworkAdapterConfiguration where IPEnabled=true get IPAddress` 是一条用于在 Windows 系统中查询启用的网络适配器及其对应的 IP 地址信息的命令。以下是对该命令的功能说明及如何将其转换为 JSON 输出的方式: ### 命令功能解释 - `wmic`: Windows Management Instrumentation Command-line 工具,用于执行 WMI(Windows Management Instrumentation)相关的管理任务。 - `path Win32_NetworkAdapterConfiguration`: 指定要操作的 WMI 类路径,`Win32_NetworkAdapterConfiguration` 表示网络适配器配置的相关数据。 - `where IPEnabled=true`: 过滤条件,仅显示启用了 IP 协议的网络适配器。 - `get IPAddress`: 获取这些网络适配器的 IP 地址。 默认情况下,WMIC 的输出是文本形式。为了将结果以 JSON 格式展示,可以通过 PowerShell 实现更灵活的数据处理和格式化。 --- ### 使用 PowerShell 将其转为 JSON 输出 可以结合 PowerShell 和 WMIC 来获取并格式化输出为 JSON 数据。例如: ```powershell Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled } | Select-Object -Property IPAddress | ConvertTo-Json ``` #### 示例输出 (JSON): ```json [ { "IPAddress": [ "192.168.1.10", "fe80::a:b:c:d" ] }, { "IPAddress": [ "172.16.0.5", "fe80::e:f:g:h" ] } ] ``` **注意**: 1. 如果系统中有多个网卡或虚拟网卡,则会返回每个启用了 IP 的网卡地址列表。 2. IPv4 和 IPv6 地址通常会同时列出。 --- ### 相关补充 如果希望直接通过 CMD 执行并将结果手动解析为 JSON,请先运行原始 WMIC 命令提取所需字段,再用脚本工具如 Python 或其他语言对结果进行处理。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后知后觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值