用C/C++實(shí)現(xiàn)linux下檢測網(wǎng)絡(luò)接口狀態(tài)
本文實(shí)例為大家分享了使用C/C++實(shí)現(xiàn)linux下檢測網(wǎng)絡(luò)接口狀態(tài),供大家參考,具體內(nèi)容如下
要寫個(gè)檢測網(wǎng)絡(luò)接口鏈接狀態(tài)的東西,又不喜歡不斷的ping別的地址,也不想調(diào)用其他命令行工具來做這個(gè),于是在google了n多內(nèi)容未果之后,搜到個(gè)檢測工具的源代碼。
以下代碼在fedora 9 / CentOS 5.2下調(diào)試通過:)
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <net/if.h>
struct ethtool_value
{
__uint32_t cmd;
__uint32_t data;
};
/*return 1:has cable; return 0:no cable*/
int detect_eth_cable(char *ifname)
{
struct ethtool_value edata;
struct ifreq ifr;
int fd = -1, err = 0;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
//perror("Cannot get control socket");
return -1;
}
edata.cmd = 0x0000000A;
ifr.ifr_data = (caddr_t)&edata;
err = ioctl(fd, 0x8946, &ifr);
if (err == 0) {
fprintf(stdout, "Link detected: %s\n", edata.data ? "yes":"no");
} else if (errno != EOPNOTSUPP) {
perror("Cannot get link status");
}
return(edata.data==1 ? 1:0);
}
int main(int argc, char**argv)
{
detect_eth_cable("p1p1");
return 0;
}
其他代碼:
int get_netportstatus(const char *interface) {
char cmd[1024];
char *tt;
FILE *fp;
int devflag;
devflag=get_netflag(interface);
if (devflag==DEV_DOWN) {
sprintf(cmd,"ifconfig %s up",interface);
system(cmd);
}
sprintf(cmd,"ethtool %s | grep \"Link detected\" > /tmp/eth.temp",interface);
system(cmd);
if (devflag==DEV_DOWN) {
sprintf(cmd,"ifconfig %s down",interface);
system(cmd);
}
fp=fopen("/tmp/eth.temp","r");
if (fp==NULL) {
system("rm -rf /tmp/eth.temp");
return -1;
}
fgets(cmd,1024,fp);
fclose(fp);
system("rm -rf /tmp/eth.temp");
tt=strstr(cmd,"no");
if (tt!=NULL) return LINK_DOWN;
tt=strstr(cmd,"yes");
if (tt!=NULL) return LINK_UP;
return -1;
}
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <net/if.h>
struct ethtool_value {
__uint32_t cmd;
__uint32_t data;
};
int main(int , char* [])
{
struct ethtool_value edata;
int fd = -1, err = 0;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, "eth0");
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("Cannot get control socket");
return 70;
}
edata.cmd = 0x0000000a;
ifr.ifr_data = (caddr_t)&edata;
err = ioctl(fd, 0x8946, &ifr);
if (err == 0) {
fprintf(stdout, "Link detected: %s\n",
edata.data ? "yes":"no");
} else if (errno != EOPNOTSUPP) {
perror("Cannot get link status");
}
return 0;
}
#include <net if.h=""> // IFF_RUNNING
//如果網(wǎng)卡已臉上網(wǎng)線,返回0,否則返回-1.
int check_nic(char *nic)
{
struct ifreq ifr;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, nic_name);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
{
return -1;
}
if(ifr.ifr_flags & IFF_RUNNING)
return 0; // 網(wǎng)卡已插上網(wǎng)線
else return -1;
}
</net>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言報(bào)錯(cuò):Format String Vulnerability的多種解決方案
Format String Vulnerability(格式化字符串漏洞)是C語言中常見且嚴(yán)重的安全漏洞之一,它通常在程序使用不受信任的輸入作為格式化字符串時(shí)發(fā)生,本文將詳細(xì)介紹Format String Vulnerability的產(chǎn)生原因,提供多種解決方案,需要的朋友可以參考下2024-06-06
C++ 動(dòng)態(tài)內(nèi)存分配詳解(new/new[]和delete/delete[])
這篇文章主要介紹了C++ 動(dòng)態(tài)內(nèi)存分配詳解(new/new[]和delete/delete[]),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
C++實(shí)現(xiàn)Matlab的zp2tf函數(shù)的示例代碼
matlab?的?zp2tf?函數(shù)的作用是將極點(diǎn)形式的?H(s)?函數(shù)的分母展開,本文主要為大家介紹了C++實(shí)現(xiàn)Matlab的zp2tf函數(shù)示例代碼,需要的可以參考一下2023-04-04
C++使用expected實(shí)現(xiàn)優(yōu)雅的錯(cuò)誤處理
C++ 中提供了很多中方式進(jìn)行錯(cuò)誤處理。無論是通過拋異常還是通過錯(cuò)誤碼,標(biāo)準(zhǔn)庫都提供相應(yīng)的調(diào)用,今天本文為大家介紹的是使用expected進(jìn)行錯(cuò)誤處理,感興趣的可以了解一下2023-06-06
C++程序的執(zhí)行順序結(jié)構(gòu)以及關(guān)系和邏輯運(yùn)算符講解
這篇文章主要介紹了C++程序的執(zhí)行順序結(jié)構(gòu)以及關(guān)系和邏輯運(yùn)算符講解,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C++實(shí)現(xiàn)藍(lán)橋杯競賽題目---搭積木
這篇文章主要介紹了C++實(shí)現(xiàn)藍(lán)橋杯競賽題目---搭積木,本篇文章通過題目分析列舉公式進(jìn)行分析算法,包含詳細(xì)的圖文,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

