用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語言線性表的鏈?zhǔn)奖硎炯皩?shí)現(xiàn)詳解
線性表的鏈?zhǔn)酱鎯μ攸c(diǎn)則是用一組任意的存儲單元存儲線性表的數(shù)據(jù)元素。這組存儲單元既可以是連續(xù)的,也可以是不連續(xù)的。本文將詳解一下C語言線性表的鏈?zhǔn)奖硎炯皩?shí)現(xiàn),感興趣的可以了解一下2022-07-07C++11 中的std::function和std::bind詳解
這篇文章主要介紹了C++ 11 std::function和std::bind,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-10-10Qt實(shí)現(xiàn)小功能之復(fù)雜抽屜效果詳解
在Qt自帶的控件中,也存在抽屜控件:QToolBar。但是,該控件有個(gè)缺點(diǎn):一次只能展開一個(gè)抽屜信息,無法實(shí)現(xiàn)多個(gè)展開。所以本文將自定義實(shí)現(xiàn)復(fù)雜抽屜效果,需要的可以參考一下2022-10-10一文帶你深入了解Qt中的順序容器類與關(guān)聯(lián)容器類
Qt中也有很多容器類,他們在存取速度、內(nèi)存開銷等方面進(jìn)行了優(yōu)化,使用起來更輕量級、更便捷,下面就跟隨小編一起來學(xué)習(xí)一下它們的具體使用吧2024-04-04C++獲得本機(jī)所有網(wǎng)卡的IP和MAC地址信息的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狢++獲得本機(jī)所有網(wǎng)卡的IP和MAC地址信息的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10