0 运行环境

  • 本机系统:Windows 10

  • 虚拟机软件:Oracle VM VirtualBox 6

  • 虚拟机系统:Ubuntu 18

1 代码

#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h> int get_localIPAndMask(char *ip,char *mask)
{ int fd, intrface, retn = 0; struct ifreq buf[INET_ADDRSTRLEN]; struct ifconf ifc; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) { ifc.ifc_len = sizeof(buf); // caddr_t,Linux内核源码里定义的:typedef void *caddr_t; ifc.ifc_buf = (caddr_t)buf; if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) {
intrface = ifc.ifc_len/sizeof(struct ifreq); while (intrface-- > 0) {
if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface])))
{ ip = (inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr)); printf("IP:%s\n", ip);
} if (!(ioctl(fd, SIOCGIFNETMASK, (char *)&buf[intrface])))
{
mask = (inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr)); printf("mask:%s\n", mask);
} } } close(fd); return 0; } } int main() {
char ip[64]; memset(ip, 0, sizeof(ip)); char mask[64]; memset(mask, 0, sizeof(mask)); get_localIPAndMask(ip,mask); return 0;
}

2 运行结果

与 ifconfig 验证一致,成功:

Linux C 获取本机所有网卡的 IP,Mask的更多相关文章

  1. linux编程获取本机网络相关参数

    getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程   ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...

  2. Linux下获取本机IP地址的代码

    Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...

  3. 用 shell 获取本机的网卡名称

    用 shell 获取本机的网卡名称 # 用 shell 获取本机的网卡名称 ls /sys/class/net # 或者 ifconfig | grep "Link" | awk ...

  4. Linux中获取本机的最新IPv6地址_更新ddns的脚本

    Linux中获取本机的最新IPv6地址_更新ddns的脚本 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-11-07. 运营商提供ipv6地址. 路由器后有台linux机器,通过e ...

  5. C++获得本机所有网卡的IP和MAC地址信息

    一台机器上可能不只有一个网卡,但每一个网卡只有一个MAC地址,而每一个网卡可能配置有多个IP地址:如平常的笔记本电脑中,就会有无线网卡和有线网卡(网线接口)两种:因此,如果要获得本机所有网卡的IP和M ...

  6. js获取本机mac地址,IP地址,计算机名

    <!DOCTYPE HTML> <html> <head> <title>js获取本机mac地址,IP地址,计算机名</title> < ...

  7. C++获取本机用于连接的IP地址

    最近写个程序需要获取本机用于连接的IP地址,经过很多的尝试后,最终使用的方法如下: 使用cmd命令    netstat  | findstr “192.168.6.66:3333” > D:\ ...

  8. 获取客户机MAC地址 根据IP地址 获取机器的MAC地址 / 获取真实Ip地址

    [DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest, Int32 host, ref ...

  9. Linux中获取本机网络信息的几个函数及应用

    一.读取/etc/hosts 几个函数 头文件<netdb.h> 1.void sethostent(int stayopen);//开打/etc/hosts 配置文件 2.struct ...

随机推荐

  1. 《流畅的Python》第二部分 数据结构 【序列构成的数组】【字典和集合】【文本和字节序列】

    第二部分 数据结构 第2章 序列构成的数组 内置序列类型 序列类型 序列 特点 容器序列 list.tuple.collections.deque - 能存放不同类型的数据:- 存放的是任意类型的对象 ...

  2. 试用 Azure Sql 数据库

    我们的12月试用账号的免费服务里有一个Azure Sql服务,最近正好自己做一个小工具需要一个数据库,正好可以把它当测试库顺便体验一把Azure Sql. 概述 Azure SQL 数据库 Azure ...

  3. go sync.WaitGroup

    package mainimport ( "fmt" "sync")func say(i int ,wg *sync.WaitGroup) { defer wg ...

  4. python selenium 自动登陆

    #-*- coding:utf8 -*- # 导入selenium2中的webdriver库 from time import sleep from selenium import webdriver ...

  5. .gdbinit文件配置

    .gdbinit文件配置 #打印数组的索引下标 set print array-indexes on #每行打印一个结构体成员 set print pretty on #除了断点有关的线程会被停下来, ...

  6. Spring Boot 学习摘要--关于配置

    date: 2019-12-27 09:00:00 updated: 2019-12-30 13:20:00 Spring Boot 学习摘要--关于配置 学习教程来自:B站 尚硅谷 1. 关于配置 ...

  7. Scala编程 笔记

    date: 2019-08-07 11:15:00 updated: 2019-11-25 20:00:00 Scala编程 笔记 1. makeRDD 和 parallelize 生成 RDD de ...

  8. maven打包插件

    如何把依赖的jar包中的资源抽到当前jar中 maven-compiler-plugin:编译插件,可指定资源jdk版本,前提是当前代码使用的jdk版本 大于或等于 source maven-asse ...

  9. 云计算管理平台之OpenStack认证服务Keystone

    一.keystone简介 keystone是openstack中的核心服务,它主要作用是实现用户认证和授权以及服务目录:所谓服务目录指所有可用服务的信息库,包含所有可用服务及其API endport路 ...

  10. 通过Python提取10000份log中的产品数据

    一.背景 协助产品部门在10000份产品log信息中提取产品的SN号.IMEI号.ICCID号到Excel表格中. 1.l原始的og内容: 2.提取后的Excel表格: 二.实现 1.思路 a.for ...