获取ethx 接口的IP地址
来源:互联网 发布:js实现div滑动效果 编辑:程序博客网 时间:2024/05/21 15:02
获取ethx 接口的IP地址
#include <stdio.h>#include <unistd.h>#include <arpa/inet.h>#include <stdlib.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/socket.h>#include <net/if.h>#include <string.h>#define MAXINTERFACES 16#define MAC_ADDR_LEN 18#define ADDR_LEN 16#define ETH_INTERFACE_NAME "eth"void err_quit(char *msg);void getIP(char *addr);int main(int argc, char *argv[]){ char addr[ADDR_LEN] = {0,}; getIP(addr); printf("%s\n", addr); return 0;}void err_quit(char *msg){ perror(msg); exit(1);}void getIP(char *addr){ int sock_fd; struct ifreq buf[MAXINTERFACES]; struct ifconf ifc; int inter_num; char *temp = NULL; if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) err_quit("Create socket failed"); ifc.ifc_len = sizeof(buf); ifc.ifc_req = buf; if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0) err_quit("Get a list of interface addresses failed"); inter_num = ifc.ifc_len / sizeof(struct ifreq); printf("The number of interfaces is %d\n", inter_num); while(inter_num--) { if(strstr(buf[inter_num].ifr_name, ETH_INTERFACE_NAME) == NULL) continue; if(ioctl(sock_fd, SIOCGIFADDR, (char *)&buf[inter_num]) < 0) err_quit("Get interface address failed"); temp = inet_ntoa(((struct sockaddr_in*)(&buf[inter_num].ifr_addr))->sin_addr); strncpy(addr, temp, ADDR_LEN); printf("IP address is %s\n", addr); }}
转载自:http://blog.chinaunix.net/uid-30441-id-2133844.html
- 获取ethx 接口的IP地址
- IP地址获取接口
- 获取指定网络接口的IP地址
- 获取调用接口的客户端ip地址
- linux 接口IP地址获取
- 根据传入接口名获取相应的ip地址
- 通过IP地址获取当前地理位置(省份)的接口
- 通过IP地址获取当前地理位置(省份)的接口
- 通过IP地址获取当前地理位置(省份)的接口
- js根据ip地址获取用户地点的接口使用方法
- javascript如何通过访问者ip地址获取ip地址对应的物理地址API接口
- IP地址的获取
- Linux 接口地址的获取-暨获取本地IP地址(所有地址,包括IPv4,IPV6,MAC 地址)
- C#获取IP地址接口编程
- 调用新浪IP接口,获取地址信息
- 根据网络接口名获取IP地址
- linux环境获取接口IP地址
- 腾讯的ip接口地址
- Asp.Net MVC4.0入门指南(6):验证编辑方法和编辑视图
- oracle 11g rac在安装时报错PRVF-4657
- 简单的PHP文件上传例子
- 多屏幕下控制台位置
- 函数和变量小知识
- 获取ethx 接口的IP地址
- IOS面试题2
- what-is-the-bios-and-what-does-it-do
- 判断两个单链表是否相交
- Boa服务器的移植——BOA移植一
- extjs和jquery
- 菜鸟学Java(十)——分页查询
- Code Fragment-UI组件的可定制化。
- 多媒体播放器