linux获取本机网卡ip地址
来源:互联网 发布:mac dock自动隐藏 编辑:程序博客网 时间:2024/05/11 19:28
#include <stdio.h>;
#include <sys/types.h>;
#include <sys/socket.h>;
#include <sys/ioctl.h>;
#include <netinet/in.h>;
#include <net/if.h>;
#include <net/if_arp.h>;
#include <arpa/inet.h>;
#include <errno.h>;
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(sock, SIOCGIFADDR, &ifr) < 0)
{
perror("ioctl");
return -1;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
fprintf(stdout, "eth0: %s\n", inet_ntoa(sin.sin_addr));
return 0;
#include <sys/types.h>;
#include <sys/socket.h>;
#include <sys/ioctl.h>;
#include <netinet/in.h>;
#include <net/if.h>;
#include <net/if_arp.h>;
#include <arpa/inet.h>;
#include <errno.h>;
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(sock, SIOCGIFADDR, &ifr) < 0)
{
perror("ioctl");
return -1;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
fprintf(stdout, "eth0: %s\n", inet_ntoa(sin.sin_addr));
return 0;
}
上面的代码需要在宏里指定要获取的网卡名。
下面的代码可以获取到所有的网卡信息。
#include <stdio.h> #include <sys/types.h> #include <sys/param.h>#include <sys/ioctl.h> #include <sys/socket.h> #include <net/if.h> #include <netinet/in.h> #include <net/if_arp.h>#ifdef SOLARIS #include <sys/sockio.h> #endif#define MAXINTERFACES 16main (argc, argv) register int argc; register char *argv[]; { register int fd, intrface, retn = 0; struct ifreq buf[MAXINTERFACES]; struct arpreq arp; struct ifconf ifc; if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0) { ifc.ifc_len = sizeof buf; ifc.ifc_buf = (caddr_t) buf; if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) { //获取接口信息 intrface = ifc.ifc_len / sizeof (struct ifreq); printf("interface num is intrface=%d\n\n\n",intrface); //根据借口信息循环获取设备IP和MAC地址 while (intrface-- > 0) { //获取设备名称 printf ("net device %s\n", buf[intrface].ifr_name); //判断网卡类型 if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface]))) { if (buf[intrface].ifr_flags & IFF_PROMISC) { puts ("the interface is PROMISC"); retn++; } } else { char str[256]; sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name); perror (str); } //判断网卡状态 if (buf[intrface].ifr_flags & IFF_UP) { puts("the interface status is UP"); } else { puts("the interface status is DOWN"); } //获取当前网卡的IP地址 if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface]))) { puts ("IP address is:"); puts(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr)); puts(""); //puts (buf[intrface].ifr_addr.sa_data); } else { char str[256]; sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name); perror (str); } /* this section can't get Hardware Address,I don't know whether the reason is module driver*/ #ifdef SOLARIS //获取MAC地址 arp.arp_pa.sa_family = AF_INET; arp.arp_ha.sa_family = AF_INET; ((struct sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr.s_addr; if (!(ioctl (fd, SIOCGARP, (char *) &arp))) { puts ("HW address is:"); //以十六进制显示MAC地址 printf("%02x:%02x:%02x:%02x:%02x:%02x\n", (unsigned char)arp.arp_ha.sa_data[0], (unsigned char)arp.arp_ha.sa_data[1], (unsigned char)arp.arp_ha.sa_data[2], (unsigned char)arp.arp_ha.sa_data[3], (unsigned char)arp.arp_ha.sa_data[4], (unsigned char)arp.arp_ha.sa_data[5]); puts(""); puts(""); }#else #if 0 /*Get HW ADDRESS of the net card */ if (!(ioctl (fd, SIOCGENADDR, (char *) &buf[intrface]))) { puts ("HW address is:"); printf("%02x:%02x:%02x:%02x:%02x:%02x\n", (unsigned char)buf[intrface].ifr_enaddr[0], (unsigned char)buf[intrface].ifr_enaddr[1], (unsigned char)buf[intrface].ifr_enaddr[2], (unsigned char)buf[intrface].ifr_enaddr[3], (unsigned char)buf[intrface].ifr_enaddr[4], (unsigned char)buf[intrface].ifr_enaddr[5]); puts(""); puts(""); } #endif if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface]))) { puts ("HW address is:"); printf("%02x:%02x:%02x:%02x:%02x:%02x\n", (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0], (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1], (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2], (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3], (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4], (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]); puts(""); puts(""); } #endif else { char str[256]; sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name); perror (str); } } //while } else perror ("cpm: ioctl"); } else perror ("cpm: socket"); close (fd); return retn; }
- linux获取本机网卡ip地址
- linux c获取本机网卡ip地址
- JAVA获取本机网卡IP地址
- Linux C语言 获取本机(所有网卡)IP地址(IPV4)
- Linux C语言 获取本机(所有网卡)IP地址(IPV4)
- Linux C语言 获取本机(所有网卡)IP地址(IPV4)
- ioctl 获取本机网卡ip地址 | socket()
- c#获取本机多网卡ip地址
- ioctl 获取本机网卡ip地址 | socket() 复制代码
- VB获取本机网卡类型、IP、MAC地址
- linux下获取本机IP地址
- linux编程获取本机IP地址
- LINUX下获取本机IP地址
- python 获取linux 本机IP地址
- linux编程获取本机IP地址
- linux编程获取本机IP地址
- Linux 获取本机IP地址
- linux shell获取本机ip地址
- 两个字符串最长的公共字串问题(LCS)
- C/C++笔试面试题
- linux shell编程实例总结
- Cannot complete the install because one or more required items could not be found
- sql中in 和 exists的用法和性能区别
- linux获取本机网卡ip地址
- Python_API_String Services_re.sub
- 内存泄漏以及常见的解决方法
- 3.Android中自定义View的应用
- mysql -- show tables的结果不一定准确
- 页面指令
- 你好,对手!
- E: Sub-process /usr/bin/dpkg returned an error code (1) 解决办法
- Three20软件引擎之结合第三方FMDB框架操作数据库详解