linux获取网卡的ip,LINUX下获取网卡IP地址和MAC地址,子网掩码程序参考

/*

mode time:20120727

LINUX下获取IP地址和MAC地址.程序相关结构体在程序后面。

打印网卡的ip地址 子网掩码 广播地址 mac地址

环境:

[root@bogon temp]# uname -a

Linux bogon 2.6.31.5-127.fc12.i686.PAE #1 SMP Sat Nov 7 21:25:57 EST 2009 i686 i686 i386 GNU/Linux

*/

#include

#include

#include

#include

#include

#include

#include

#include

#ifdef SOLARIS

#include

#endif

#define MAXINTERFACES 16 /* 最大接口数 */

int main(int argc, char **argv)

{

register int fd, intrface, retn = 0;

struct ifreq buf[MAXINTERFACES]; /* ifreq结构数组 */

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",intrface);

puts("");

//根据借口信息循环获取设备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])))

{

printf("IP address is:");

puts((char *)inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));

//printf("\n%d\n"buf[intrface].ifr_addr))->sin_addr.s_addr);

//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)))

{

printf("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])))

{

printf("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("");

}

#endif

if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))

{

printf("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]);

}

#endif

else

{

char str[256];

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

perror (str);

}

//子网掩码

if (!(ioctl(fd, SIOCGIFNETMASK, (char *) &buf[intrface])))

{

printf("MASK:%s",

(char*)inet_ntoa(((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));

puts("");

}

else

{

char str[256];

sprintf(str, "SIOCGIFADDR ioctl %s", buf[intrface].ifr_name);

perror(str);

}

//广播地址

if (! (ioctl(fd, SIOCGIFBRDADDR, (char *) &buf[intrface])))

printf("Broadcast Address:%s\n",

(char*)inet_ntoa(((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));

puts("");

puts("");

} //while

} else

perror ("cpm: ioctl");

} else

perror ("cpm: socket");

close (fd);

return retn;

}

/*

#include

int ioctl(int fd, int request, … * void *arg *);

返回:成功返回0,失败返回-1

char *inet_ntoa (struct in_addr);

返回点分十进制的字符串在静态内存中的指针。

所在头文件: 函数功能:将网络地址转换成“.”点隔的字符串格式。

所需库: winsock.h

#include

struct ifconf

{

intifc_len;/* size of buffer*

union

{

char __user *ifcu_buf;

struct ifreq __user *ifcu_req;

} ifc_ifcu;

};

struct ifreq

{

#define IFHWADDRLEN6

union

{

charifrn_name[IFNAMSIZ];* if name, e.g. "en0" *

} ifr_ifrn;

union {

structsockaddr ifru_addr;

structsockaddr ifru_dstaddr;

structsockaddr ifru_broadaddr;

structsockaddr ifru_netmask;

struct sockaddr ifru_hwaddr;

shortifru_flags;

intifru_ivalue;

intifru_mtu;

struct ifmap ifru_map;

charifru_slave[IFNAMSIZ];/* Just fits the size *

charifru_newname[IFNAMSIZ];

void __user *ifru_data;

structif_settings ifru_settings;

} ifr_ifru;

};

#define ifr_nameifr_ifrn.ifrn_name/* interface name *

#define ifr_hwaddrifr_ifru.ifru_hwaddr/* MAC address *

#defineifr_addrifr_ifru.ifru_addr/* address*

#defineifr_dstaddrifr_ifru.ifru_dstaddr/* other end of p-p lnk*

#defineifr_broadaddrifr_ifru.ifru_broadaddr/* broadcast address*

#defineifr_netmaskifr_ifru.ifru_netmask/* interface net mask*

#defineifr_flagsifr_ifru.ifru_flags/* flags*

#defineifr_metricifr_ifru.ifru_ivalue/* metric*

#defineifr_mtuifr_ifru.ifru_mtu/* mtu*

#define ifr_mapifr_ifru.ifru_map/* device map

#define ifr_slaveifr_ifru.ifru_slave/* slave device*

#defineifr_dataifr_ifru.ifru_data/* for use by interface*

#define ifr_ifindexifr_ifru.ifru_ivalue/* interface index

#define ifr_bandwidthifr_ifru.ifru_ivalue /* link bandwidth*

#define ifr_qlenifr_ifru.ifru_ivalue/* Queue length *

#define ifr_newnameifr_ifru.ifru_newname/* New name*

#define ifr_settingsifr_ifru.ifru_settings/* Device/proto settings*

*/

[root@bogon temp]# gcc -g -o get_ip get_ip.c [root@bogon temp]# ./get_ip interface num is intrface=2 net device eth1 the interface status is UP IP address is:192.168.94.125 HW address is:00:0c:29:26:e4:f2 MASK:255.255.255.0 Broadcast Address:192.168.94.255 net device lo the interface status is UP IP address is:127.0.0.1 HW address is:00:00:00:00:00:00 MASK:255.0.0.0 Broadcast Address:0.0.0.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值