LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 604|回复: 6

我想取得Linux系统中网卡的MAC地址.

[复制链接]
发表于 2005-9-8 17:40:33 | 显示全部楼层 |阅读模式
我想取得Linux系统中网卡的MAC地址,取得这个地址后做加密之用,请问怎做呢?
发表于 2005-9-8 17:53:14 | 显示全部楼层
试试这个
groups.google.com
回复 支持 反对

使用道具 举报

发表于 2005-9-8 17:53:20 | 显示全部楼层

试试这个

#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>

#define MAXINTERFACES 16

main (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);
while (intrface-- > 0)
{
printf ("net device %s\n", buf[intrface].ifr_name);

/*Jugde whether the net card status is promisc*/
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);
}

/*Jugde whether the net card status is up*/
if (buf[intrface].ifr_flags & IFF_UP) {
puts("the interface status is UP");
}
else {
puts("the interface status is DOWN");
}

/*Get IP of the net card */
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);
}

/*Get HW ADDRESS of the net card */
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("");
}

else {
char str[256];

sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
perror (str);
}
}
} else
perror ("cpm: ioctl");

} else
perror ("cpm: socket");

close (fd);
return retn;
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-9 08:05:21 | 显示全部楼层
谢谢,以后有好东东我也给大家分享!
回复 支持 反对

使用道具 举报

发表于 2005-9-9 09:26:55 | 显示全部楼层
说说你的加密算法吧  
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-9 11:40:34 | 显示全部楼层
不足挂齿的算法,就不提了。
回复 支持 反对

使用道具 举报

发表于 2005-9-9 12:14:06 | 显示全部楼层
显然是做License限制,大家不要告诉她。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表