Linux c 检测当前网卡是否已经启动
思路:
1、socket 建立一个数据报套接字。
2、定义一个struct ifreq ifr 结构体。将网络名称如“eth0” 赋值给ifr结构体的ifr.ifr_name。
3、调用ioctl(sockfd, SIOCGIFFLAGS, &ifr) 获取网络标识。
如需设置网络标识,更改ifr结构体调用ioctl(sockfd, SIOCSIFFLAGS, &ifr) 进行设置。
4、比对网络标识来确定网络是否正在运行。ifr.ifr_ifru.ifru_flags &IFF_RUNNING。
int GetNetStatus(const char *ifname)
{
struct ifreq ifr;
int sockfd; if((sockfd = Socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("socket create error!\n");
return - 1;
} memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); if(ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
{
Close(sockfd);
return - 1;
}
Close(sockfd);
if(ifr.ifr_ifru.ifru_flags &IFF_RUNNING)
{
return 1;
}
else
{
return 0;
} }
struct ifreq 结构体的详细介绍可参考。
struct ifreq学习和实例 - 走看看 (zoukankan.com)
这个结构定义在/usr/include/net/if.h,用来配置和获取ip地址,掩码,MTU等接口信息的。
/* Interface request structure used for socket ioctl's. All interface
ioctl's must have parameter definitions which begin with ifr_name.
The remainder may be interface specific. */
struct ifreq
{
# define IFHWADDRLEN 6
# define IFNAMSIZ IF_NAMESIZE
union
{
char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
} ifr_ifrn;
union
{
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
struct sockaddr ifru_netmask;
struct sockaddr ifru_hwaddr;
short int ifru_flags;
int ifru_ivalue;
int ifru_mtu;
struct ifmap ifru_map;
char ifru_slave[IFNAMSIZ]; /* Just fits the size */
char ifru_newname[IFNAMSIZ];
__caddr_t ifru_data;
} ifr_ifru;
};
# define ifr_name ifr_ifrn.ifrn_name /* interface name */
# define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */
# define ifr_addr ifr_ifru.ifru_addr /* address */
# define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
# define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
# define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */
# define ifr_flags ifr_ifru.ifru_flags /* flags */
# define ifr_metric ifr_ifru.ifru_ivalue /* metric */
# define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
# define ifr_map ifr_ifru.ifru_map /* device map */
# define ifr_slave ifr_ifru.ifru_slave /* slave device */
# define ifr_data ifr_ifru.ifru_data /* for use by interface */
# define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */
# define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */
# define ifr_qlen ifr_ifru.ifru_ivalue /* queue length */
# define ifr_newname ifr_ifru.ifru_newname /* New name */
# define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
# define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
# define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)

实例一,获取网卡的IP地址:
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
int inet_sock;
struct ifreq ifr;
inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, "eth0");
//SIOCGIFADDR标志代表获取接口地址
if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0)
perror("ioctl");
printf("%s
", inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
return 0;
}
实例二,实现简单ifconfig功能:
/**
* file getifstat.c
* author wzj
* rief 访问这个struct ifconf 修改,查询状态
* version
*
ote
* date: 2012年08月11日星期六22:55:25
*/
#include <net/if.h> /* for ifconf */
#include <linux/sockios.h> /* for net status mask */
#include <netinet/in.h> /* for sockaddr_in */
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#define MAX_INTERFACE (16)
void port_status(unsigned int flags);
/* set == 0: do clean , set == 1: do set! */
int set_if_flags(char *pif_name, int sock, int status, int set)
{
struct ifreq ifr;
int ret = 0;
strncpy(ifr.ifr_name, pif_name, strlen(pif_name) + 1);
ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
if(ret)
return -1;
/* set or clean */
if(set)
ifr.ifr_flags |= status;
else
ifr.ifr_flags &= ~status;
/* set flags */
ret = ioctl(sock, SIOCSIFFLAGS, &ifr);
if(ret)
return -1;
return 0;
}
int get_if_info(int fd)
{
struct ifreq buf[MAX_INTERFACE];
struct ifconf ifc;
int ret = 0;
int if_num = 0;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t) buf;
ret = ioctl(fd, SIOCGIFCONF, (char*)&ifc);
if(ret)
{
printf("get if config info failed");
return -1;
}
/* 网口总数 ifc.ifc_len 应该是一个出入参数 */
if_num = ifc.ifc_len/sizeof(struct ifreq);
printf("interface num is interface = %d
", if_num);
while(if_num-- > 0)
{
printf("net device: %s
", buf[if_num].ifr_name);
/* 获取第n个网口信息 */
ret = ioctl(fd, SIOCGIFFLAGS, (char*)&buf[if_num]);
if(ret)
continue;
/* 获取网口状态 */
port_status(buf[if_num].ifr_flags);
/* 获取当前网卡的ip地址 */
ret = ioctl(fd, SIOCGIFADDR, (char*)&buf[if_num]);
if(ret)
continue;
printf("IP address is:
%s
", inet_ntoa(((struct sockaddr_in *)(&buf[if_num].ifr_addr))->sin_addr));
/* 获取当前网卡的mac */
ret = ioctl(fd, SIOCGIFHWADDR, (char*)&buf[if_num]);
if(ret)
continue;
printf("%02x:%02x:%02x:%02x:%02x:%02x
",
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[0],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[1],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[2],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[3],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[4],
(unsigned char)buf[if_num].ifr_hwaddr.sa_data[5]
);
}
}
void port_status(unsigned int flags)
{
if(flags & IFF_UP)
{
printf("is up
");
}
if(flags & IFF_BROADCAST)
{
printf("is broadcast
");
}
if(flags & IFF_LOOPBACK)
{
printf("is loop back
");
}
if(flags & IFF_POINTOPOINT)
{
printf("is point to point
");
}
if(flags & IFF_RUNNING)
{
printf("is running
");
}
if(flags & IFF_PROMISC)
{
printf("is promisc
");
}
}
int main()
{
int fd;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd > 0)
{
get_if_info(fd);
close(fd);
}
return 0;
}
运行结果:
interface num is interface = 2
net device: eth0
is up
is broadcast
is running
IP address is:
192.168.100.200
54:be:f7:33:57:26
net device: lo
is up
is loop back
is running
IP address is:
127.0.0.1
00:00:00:00:00:00
参考转载博客地址:
http://blog.csdn.net/dsg333/article/details/7525634
http://blog.csdn.net/kulung/article/details/6442597
http://blog.csdn.net/joker0910/article/details/7855998
Linux c 检测当前网卡是否已经启动的更多相关文章
- (笔记)Linux下检测网卡与网线连接状态
http://blog.chinaunix.net/space.php?uid=20357359&do=blog&cuid=1798479 Linux下检测网卡与网线连接状态,使用io ...
- linux本地检测tomcat是否启动成功
@参考文章 原文如下: linux本地检测如何tomcat是否启动成功? 解决方法: 1.curl 127.0.0.1:8080 第一可以知道本地是否可以访问tomcat,返回页面代码 2.tail ...
- linux下安装编译网卡驱动的方法
安装linux操作系统后发现没有网卡驱动,表现为 system → Administration → Network下Hardware列表为空. 以下为安装编译网卡驱动的过程,本人是菜鸟,以下是我从网 ...
- Linux网络管理之多网卡绑定
一.bonding介绍 在企业Linux服务器管理里中,服务器的可靠性.可用性以及I/O速度都非常重要,保持服务器的高可用和安全性是生产环境的重要指标,其中最重要的一点是服务器网络连接的高可用性.通常 ...
- ARM linux解析之压缩内核zImage的启动过程
ARM linux解析之压缩内核zImage的启动过程 semilog@163.com 首先,我们要知道在zImage的生成过程中,是把arch/arm/boot/compressed/head.s ...
- Linux 入侵检测小结
Linux 入侵检测小结 0x00 审计命令 在linux中有5个用于审计的命令: last:这个命令可用于查看我们系统的成功登录.关机.重启等情况:这个命令就是将/var/log/wtmp文件格式 ...
- Linux 操作系统下为网卡配置ip
Linux操作系统下为网卡配置ip by:授客 QQ:1033553122 1. Linux单一网卡设置多IP的配置方法 在Linux下网卡接口逻辑名被称为eth0,eth1,eth2,..... ...
- Linux中实现多网卡绑定总结
在Linux中实现多网卡绑定 一.原理介绍: 1.什么是bonding? Linux bonding 驱动提供了一个把多个网络接口设备捆绑为单个的网络接口设置来使用.用于网络负载均衡及网络冗余: Li ...
- Linux 服务器下多网卡的负载均衡
Linux 服务器下多网卡负载均衡的实现 一.引言 现今几乎各行各业内部都建立了自己的服务器,由于服务器的特殊地位,它的可靠性.可用性及其 I/O 速度就显得非常的重要, 保持服务器的高可用 ...
随机推荐
- Springboot JSON 转换:Jackson篇
本案例基于 Springboot 2.5.7 单元测试场景下进行 <!-- SpringMVC默认使用Jacson,只需要引用web启动器即可,无序单独引用Jackson --> < ...
- C#-多线程的使用Tread
首先是概念,什么是线程? 线程是操作系统分配CPU时间的基本单元,在一个进程中可以有多个线程同时执行代码. 谈一谈什么是进程? 简单的说,一个正在运行的应用程序可以视为一个进程,进程间相互独立,资源不 ...
- 1.WEB应用模式
1. Web应用模式 在开发Web应用中,有两种应用模式: 前后端不分离[客户端看到的内容和所有界面效果都是由服务端提供出来的.] 前后端分离[把前端的界面效果(html,css,js分离到另一个服务 ...
- UBOOT编译--- make xxx_deconfig过程详解(一)
make xxx_deconfig过程详解 1. 前言 2. 概述 3. build变量的定义 4. 目标%config的定义 4.1 依赖 scripts_basic 4.1.1 语句$(if $ ...
- JavaScript实现带省略号的分页
1.实现功能 可以跳转上一页.下一页.数据过多省略号显示,点击省略号可以实现快速跳转. 纯js+html+css实现,引入js文件后再使用方法即可快速生成. 2.实现过程 2.1 html页面(ind ...
- 小菜鸡的学习笔记---<正则表达式(1)>
正则表达式学习笔记(1) (纯新手学习笔记,大佬绕路 QAQ) 一.简介 正则表达式就是一种文本模式用来匹配一系列满足特定条件的字符串,可以对比一下数学里面的表达式,比如我们要用一个表达式表示一串数字 ...
- 重新整理 .net core 实践篇 ———— linux上性能排查 [外篇]
前言 该文的前置篇为: https://www.cnblogs.com/aoximin/p/16839830.html 本文介绍性能排查. 正文 上一节是出现错误了,如何去排查具体问题. 这一节介绍一 ...
- MLP(SGD or Adam) Perceptron Neural Network Working by Pytorch(including data preprocessing)
通过MLP多层感知机神经网络训练模型,使之能够根据sonar的六十个特征成功预测物体是金属还是石头.由于是简单的linearr线性仿射层,所以网络模型的匹配度并不高. 这是我的第一篇随笔,就拿这个来练 ...
- Android开发之应用更新或软件下载
Android开发之应用更新或软件下载 本文章学习前提:okHttp3或以上,EventBus或其它事件总线工具,四大组件的Activity和Service,安卓通知基础知识 新建项目文件 目录结构如 ...
- 基于python的数学建模---多模糊评价
权重 ak的确定--频数统计法 选取正整数p的方法 画箱形图 取1/4与3/4的距离(IQR) ceil()取整 代码: import numpy as np def frequency(mat ...