ubuntu(Linux) c++ 获取本机IPv4和ipv6、查询本机IPv4,IPv6
1.关于
演示环境:
Linux xxxxxxx 5.4.0-47-generic #51-Ubuntu SMP Fri Sep 4 19:50:52 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
代码来自 StackOverflow But,自己做了部分修改。
2.头文件
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
3.ipv4获取
调用函数get_ip_linux可以获取本机ipv4
int net_adapter_helper::get_ipv4_linux(std::list<std::string>& out_list_ip4)
{
return get_ip_linux(AF_INET, out_list_ip4);
}
4.ipv6获取
调用函数get_ip_linux可以获取本机ipv6
int net_adapter_helper::get_ipv6_linux(std::list<std::string>& out_list_ip6)
{
return get_ip_linux(AF_INET6, out_list_ip6);
}
5.get_ip_linux封装
这里封装,目的是为了更方便维护获取本机IP的代码。通过第一个参数控制当前需要获取ipv4还是ipv6。
int net_adapter_helper::get_ip_linux(int ipv4_6, std::list<std::string>& out_list_ip)
{
int ret_val = 0;
struct ifaddrs * ifAddrStruct = NULL;
void * tmpAddrPtr = NULL;
// 1.
ret_val = getifaddrs(&ifAddrStruct);
if (0 != ret_val)
{
ret_val = errno;
return ret_val;
}
// 2.
std::string str_ipvX;
int padress_buf_len = 0;
char addressBuffer[INET6_ADDRSTRLEN] = {0};
if (AF_INET6 == ipv4_6)
padress_buf_len = INET6_ADDRSTRLEN;
else
padress_buf_len = INET_ADDRSTRLEN;
while (NULL != ifAddrStruct )
{
if (ipv4_6 == ifAddrStruct->ifa_addr->sa_family )
{
// is a valid IP4 Address
tmpAddrPtr = &((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
inet_ntop(ipv4_6, tmpAddrPtr, addressBuffer, padress_buf_len);
str_ipvX = std::string(addressBuffer);
out_list_ip.push_back(str_ipvX);
memset(addressBuffer, 0, padress_buf_len);
}
ifAddrStruct=ifAddrStruct->ifa_next;
}
return ret_val;
}
6.测试结果
- 6.1 调用代码
std::list<std::string> ipv4;
std::list<std::string> ipv6;
int ret_val = net_adapter_helper::get().get_ipv4_linux(ipv4);
if (0 != ret_val)
{
cout << "error, ipv4, id = " << ret_val << endl;
}
else
{
int index = 0;
for (auto item: ipv4)
{
cout << "index = " << ++index;
cout << ", ipv4 = " << item.c_str() << endl;
}
}
ret_val = net_adapter_helper::get().get_ipv6_linux(ipv6);
if (0 != ret_val)
{
cout << "error, ipv6, id = " << ret_val << endl;
}
else
{
int index = 0;
for (auto item: ipv6)
{
cout << "index = " << ++index;
cout << ", ipv6 = " << item.c_str() << endl;
}
}
- 6.2 输出结果:

ubuntu(Linux) c++ 获取本机IPv4和ipv6、查询本机IPv4,IPv6的更多相关文章
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
- Linux中获取本机的最新IPv6地址_更新ddns的脚本
Linux中获取本机的最新IPv6地址_更新ddns的脚本 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-11-07. 运营商提供ipv6地址. 路由器后有台linux机器,通过e ...
- Linux下获取本机IP地址的代码
Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...
- linux下获取软件源码包 centos/redhat, debian/ubuntu
linux下获取软件源码包 centos/redhat, debian/ubuntu centos下: 1. yum install yum-utils 主要为了获取yumdownloader 2. ...
- windows10 c++获取本机IPv4,ipv6,查询本机Ipv4,ipv6
1.关于 演示环境: win10+VS2017 2.支持 需要下面的库支持: ws2_32.lib 需要下面的宏,添加到项目属性-> c/c++ -> 预处理器定义 _WINSOCK_DE ...
- Ubuntu Linux下设置IP的配置命令
Ubuntu Linux下设置IP的配置命令 今天装了Ubuntu,但是发现不能上网,开始排查问题: 1.首先确定网络连接是否正确,所用的网线是否可以正常工作 2.查看网卡是否能正常工作,检测的方法如 ...
- HOWTO install Oracle 11g on Ubuntu Linux 12.04 (Precise Pangolin) 64bits
安装了Ubuntu 12.04 64bit, 想在上面安装Oracle 11gr2,网上找了好多文档都没成功,最后完全参考了MordicusEtCubitus的文章. 成功安装的关键点:install ...
- [r]Ubuntu Linux系统下apt-get命令详解
Ubuntu Linux系统下apt-get命令详解(via|via) 常用的APT命令参数: apt-cache search package 搜索包 apt-cache show package ...
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
// all.h// 2005/06/20,a.m. wenxy #ifndef _ALL_H#define _ALL_H #include <memory.h>#include < ...
随机推荐
- Python三元表达式,列表推导式,字典生成式
目录 1. 三元表达式 2. 列表推导式 3. 字典生成式 3.1 字典生成式 3.2 zip()方法 1. 三元表达式 """ 条件成立时的返回值 if 条件 else ...
- mysql—MySQL数据库中10位时间戳转换为标准时间后,如何对标准时间进行加减X天处理
在这篇的缘由:问题:"FROM_UNIXTIME(timeline,'%Y-%m')"的结果(2020-06)做月份增加1月或者减少1月的计算处理,想着直接在结果上+1但是,结果为 ...
- Redis | 第10章 二进制数组、慢查询日志和监视器《Redis设计与实现》
目录 前言 1. 二进制位数组 1.1 位数组的表示 1.2 GETBIT 命令的实现 1.3 SETBIT 命令的实现 1.4 BITECOUNT 命令的实现 1.5 BITOP 命令的实现 2. ...
- oracle加密encrypt,解密decrypt
目录 oracle加密encrypt,解密decrypt 加密 解密 oracle加密encrypt,解密decrypt 有的oracle版本没有加解密函数,以下操作可以手动添加 oracle数据使用 ...
- 【Python】【Basic】【数据类型】运算符与深浅拷贝
运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 三元运算 三元运算(三目运算),是对简单的条件语句的缩写. # 书写格式 result = 值1 if 条件 ...
- Mave 下载与安装
一,Maven 介绍 我们在开发中经常需要依赖第三方的包,包与包之间存在依赖关系,版本间还有兼容性问题,有时还需要将旧的包升级或降级,当项目复杂到一定程度时包管理变得非常重要.Maven是当前最受欢迎 ...
- 【C/C++】例题 4-2 刽子手游戏/算法竞赛入门经典/函数和递归
[题目] 猜单词游戏. 计算机想一个单词让你猜,你每次猜一个字母. 如果单词里有那个[字母],[所有该字母会显示出来]. 如果没有那个字母,算猜错一次.[最多只能猜错六次] 猜一个已经猜过的字母也算错 ...
- Apache Log4j 2 报高危漏洞,CODING 联手腾讯安全护卫软件安全
导语 12 月 9 日晚间,Apache Log4j 2 发现了远程代码执行漏洞,恶意使用者可以通过该漏洞在目标服务器上执行任意代码,危害极大. 腾讯安全第一时间将该漏洞收录至腾讯安全漏洞特征库中,C ...
- rpm-build方式制作rpm包
目录 一.简介 二.具体操作 一.简介 可以将编译完成的服务打成rpm包放到私有仓库了,用于自定义的各种软件进行安装部署配置. 二.具体操作 1.安装软件,这个命令将构建rpm包 yum -y ins ...
- [BUUCTF]PWN9——ciscn_2019_en_2
[BUUCTF]PWN9--ciscn_2019_en_2 题目网址:https://buuoj.cn/challenges#ciscn_2019_en_2 步骤: 例行检查,64位,开启了NX保护 ...