请问如何得到网卡的MAC地址等信息。

cx820402 2010-11-01 09:34:32
RT。请问如何得到网卡的MAC地址、IP地址、子网掩码、默认网关等信息呢。谢谢。
...全文
98 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Linux-Torvalds 2010-11-01
  • 打赏
  • 举报
回复
ifconfig eth0 就可以了。
freetstar 2010-11-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 steptodream 的回复:]

每个口对应的MAC ifconfig | grep HWaddr | awk '{print $1"->"$5}'
IP 广播 掩码 ifconfig | grep "Bcast:"
默认网关 route | grep "default" | awk '{print $2}'
[/Quote]
主席的好简洁
freetstar 2010-11-01
  • 打赏
  • 举报
回复
ifconfig。。。。
baocuo1314 2010-11-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 steptodream 的回复:]

每个口对应的MAC ifconfig | grep HWaddr | awk '{print $1"->"$5}'
IP 广播 掩码 ifconfig | grep "Bcast:"
默认网关 route | grep "default" | awk '{print $2}'
[/Quote]

荡漾!!
steptodream 2010-11-01
  • 打赏
  • 举报
回复
每个口对应的MAC ifconfig | grep HWaddr | awk '{print $1"->"$5}'
IP 广播 掩码 ifconfig | grep "Bcast:"
默认网关 route | grep "default" | awk '{print $2}'
linsen_519 2010-11-01
  • 打赏
  • 举报
回复
MAC:ifconfig -a | sed -n 's/.*\([[:xdigit:]]\{2\}\(:[[:xdigit:]]\{2\}\)\{5\}\).*/\1/gp'
IP:ifconfig -a | grep 'inet ' | cut -d ':' -f 2 |cut -d ' ' -f 1 | grep -v '^127'

道理很简单~就是从ifconfig上面去分割。。。其他的也一样
zynh1020 2010-11-01
  • 打赏
  • 举报
回复
ifconfig 好像就可以把
cx820402 2010-11-01
  • 打赏
  • 举报
回复
就像你这样获取IP地址,是最合适的。其他获取MAC类似的就可以了。
如果写到文件里,再用fstream读取的话,我觉得速度可能是个问题。

[Quote=引用 10 楼 linsen_519 的回复:]

你把他们运行的结果写到文件里,再用fstream 读取不行吗?

下面这是获取IP的
C/C++ code
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/i……
[/Quote]
快乐田伯光 2010-11-01
  • 打赏
  • 举报
回复
不般这种跟device的接口都用ioctl
linsen_519 2010-11-01
  • 打赏
  • 举报
回复
你把他们运行的结果写到文件里,再用fstream 读取不行吗?

下面这是获取IP的
#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>
#include <string.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;
}
steptodream 2010-11-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 cx820402 的回复:]

有没有类似windows下用IPHlpAPI这样的接口函数来得到这些信息呢?我说的是用C/C++语言。
[/Quote]
这个博客里有源码http://blog.csdn.net/hemmanhui/archive/2009/06/30/4308693.aspx
cx820402 2010-11-01
  • 打赏
  • 举报
回复
有没有类似windows下用IPHlpAPI这样的接口函数来得到这些信息呢?我说的是用C/C++语言。

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧