C語(yǔ)言實(shí)現(xiàn)linux網(wǎng)卡連接檢測(cè)的方法
本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)linux網(wǎng)卡連接檢測(cè)的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼吧
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/mii.h>
#include <linux/sockios.h>
#include <errno.h>
int get_if_miireg(const char *if_name, int phy_id, int reg_num )
{
int fd = -1;
struct ifreq ifr;
struct mii_ioctl_data *mii;
int value;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
close(fd);
return -1;
}
bzero(&ifr, sizeof(ifr));
strncpy(ifr.ifr_name, if_name, IFNAMSIZ-1);
ifr.ifr_name[IFNAMSIZ-1] = 0;
if (ioctl(fd, SIOCGMIIPHY, &ifr) < 0)
{
perror("ioctl");
close(fd);
return -1;
}
mii = (struct mii_ioctl_data *)&ifr.ifr_data;
mii->reg_num = reg_num;//0x01
if (ioctl(fd, SIOCGMIIREG, &ifr) < 0)
{
perror("ioctl");
close(fd);
return -1;
}
close(fd);
value = ((mii->val_out&0x04)>>2);
return value;
}
int main(int argc, char* argv[])
{
int i=0;
if(argc != 2)
{
fprintf(stderr, "usage: %s <ethname>", argv[0]);
return -1;
}
i = get_if_miireg(argv[1],0x10,0x01);
printf( "if_status = %d\n", i );
return 0;
}
只能識(shí)別網(wǎng)線是否連接,還沒(méi)識(shí)別網(wǎng)卡是否存在狀態(tài),也不識(shí)別網(wǎng)卡存在是否為down狀態(tài)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Visual Studio2019調(diào)試DLL的實(shí)現(xiàn)
本文主要介紹了Visual Studio2019調(diào)試DLL的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
C/C++?Qt?數(shù)據(jù)庫(kù)QSql增刪改查組件應(yīng)用教程
Qt?SQL模塊是Qt中用來(lái)操作數(shù)據(jù)庫(kù)的類,該類封裝了各種SQL數(shù)據(jù)庫(kù)接口,可以很方便的鏈接并使用。本文主要介紹了Qt數(shù)據(jù)庫(kù)QSql增刪改查組件的應(yīng)用教程,感興趣的同學(xué)可以學(xué)習(xí)一下2021-12-12
Windows環(huán)境給FFmpeg集成AVS3解碼器
libuavs3d是AVS3標(biāo)準(zhǔn)的解碼器,支持windows/linux/arm/ios等所有常用平臺(tái),在移動(dòng)端最高支持4K/30fps視頻實(shí)時(shí)解碼,解碼速度大幅領(lǐng)先AV1開(kāi)源解碼器dav1d和aomdec,由于FFmpeg默認(rèn)未啟用libuavs3d,因此需要重新配置FFmpeg,標(biāo)明啟用libuavs3d,然后重新編譯安裝FFmpeg2024-05-05

