#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> /********************************************************************** * 函数名称: GetNetStat * 功能描述: 检测网络链接是否断开 * 输入参数: * 输出参数: 无 * 返 回 值: 正常链接1,断开返回-1 * 其它说明: 本程序需要超级用户权限才能成功调用ifconfig命令 ***********************************************************************/ int GetNetStat( ) { char buffer[BUFSIZ]; FILE *read_fp; int chars_read; int ret; memset( buffer, 0, BUFSIZ ); read_fp = popen("ifconfig eth0 | grep RUNNING", "r"); if ( read_fp != NULL ) { chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp); if (chars_read > 0) { ret = 1; } else { ret = -1; } pclose(read_fp); } else { ret = -1; } return ret; } int main() { int i=0; i = GetNetStat(); printf( "\nNetStat = %d\n", i ); return 0; }
#include <stdio.h>#include <sys/ioctl.h>#include <sys/socket.h>#include <sys/types.h>#include <netdb.h>#include <net/if.h>#include <arpa/inet.h>static int get_hostip(char *storeip){ int sfd, intr; struct ifreq buf[16]; struct ifconf ifc; sfd = socket (AF_INET, SOCK_DGRAM, 0); if (sfd < 0) return -1; ifc.ifc_len = sizeof(buf); ifc.ifc_buf = (caddr_t)buf; if (ioctl(sfd, SIOCGIFCONF, (char *)&ifc)) return -1; intr = ifc.ifc_len / sizeof(struct ifreq); while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char *)&buf[intr])); close(sfd); strcpy(storeip, inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr));return 0;}
/*获取mac地址*/#include <stdio.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <netinet/in.h> #include <net/if.h> #include <string.h> static int GetMacAddr(char *WhichEth, char *MacAdd) { struct ifreq IfReq; int sock; if (NULL == MacAdd || NULL == WhichEth){ return -1; } if((sock = socket(AF_INET,SOCK_STREAM, 0)) < 0){ perror("socket"); return -2; } strcpy(IfReq.ifr_name, WhichEth); if(ioctl(sock,SIOCGIFHWADDR,&IfReq) < 0){ perror("ioctl"); return -3; } sprintf(MacAdd, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char)IfReq.ifr_hwaddr.sa_data[0], (unsigned char)IfReq.ifr_hwaddr.sa_data[1], (unsigned char)IfReq.ifr_hwaddr.sa_data[2], (unsigned char)IfReq.ifr_hwaddr.sa_data[3], (unsigned char)IfReq.ifr_hwaddr.sa_data[4], (unsigned char)IfReq.ifr_hwaddr.sa_data[5]); return 0; }
#include <arpa/inet.h> #include <arpa/nameser.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <ctype.h> #include <ctype.h> #include <errno.h> #include <netdb.h> #include <resolv.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ioctl.h> #include <net/if.h> int GetIpAddr(const char *EthName, char *IpAddr) { int Ret = -1; int Fd = 0; int Intrface = 0; struct ifconf Ifc; struct ifreq IfreqBuff[16]; if (0 > (Fd = socket(AF_INET, SOCK_DGRAM, 0))){ goto ERR_EXIT; } Ifc.ifc_len = sizeof(IfreqBuff); Ifc.ifc_buf = (caddr_t)IfreqBuff; if(0 > ioctl(Fd, SIOCGIFCONF, (char *)&Ifc)){ goto ERR_EXIT; } Intrface = Ifc.ifc_len / sizeof(struct ifreq); while (Intrface-- > 0){ if (strstr(IfreqBuff[Intrface].ifr_name, EthName)){ if (0 > ioctl(Fd, SIOCGIFADDR, (char *)&IfreqBuff[Intrface])){ goto ERR_EXIT; } //sprintf (IpAddr, "%s", inet_ntoa(((struct sockaddr_in *)(&IfreqBuff[Intrface].ifr_addr))->sin_addr)); sprintf (IpAddr, "%s", inet_ntoa(((struct sockaddr_in *)(&IfreqBuff[Intrface].ifr_hwaddr))->sin_addr)); //printf ("addr [%s]", IpAddr); } sprintf (IpAddr, "%s", inet_ntoa(((struct sockaddr_in *)(&IfreqBuff[Intrface].ifr_dstaddr))->sin_addr)); printf ("addr [%s]", IpAddr); } return 0; ERR_EXIT: return -1; } int GetMaskAddr(const char *EthName, char *MaskAddr) { int Ret = -1; int Fd = 0; int Intrface = 0; struct ifconf Ifc; struct ifreq IfreqBuff[16]; if (0 > (Fd = socket(AF_INET, SOCK_DGRAM, 0))){ goto ERR_EXIT; } Ifc.ifc_len = sizeof(IfreqBuff); Ifc.ifc_buf = (caddr_t)IfreqBuff; if(0 > ioctl(Fd, SIOCGIFCONF, (char *)&Ifc)){ goto ERR_EXIT; } Intrface = Ifc.ifc_len / sizeof(struct ifreq); while (Intrface-- > 0){ if (strstr(IfreqBuff[Intrface].ifr_name, EthName)){ if (0 > ioctl(Fd, SIOCGIFNETMASK, (char *)&IfreqBuff[Intrface])){// SIOCGIFADDR goto ERR_EXIT; } sprintf (MaskAddr, "%s", inet_ntoa(((struct sockaddr_in *)(&IfreqBuff[Intrface].ifr_netmask))->sin_addr)); } } return 0; ERR_EXIT: return -1; } //¹ã²¥ SIOCGIFBRDADDR //ip SIOCGIFADDR //×ÓÍøÑÚÂë SIOCGIFNETMASK //io = ioctl(sockfd, SIOCGIFHWADDR, (char *)buffer); int main() { char ip[20]; char mask[20]; GetIpAddr("eth0",ip); puts(ip); GetMaskAddr("eth0",mask); puts(mask); }
NETWORK
最新推荐文章于 2024-02-02 17:20:01 发布