(1)从核心交换机上查看,mac+IP地址对应列表,查看形成的文档化的,服务器交换机网络连接对应表
(2)获取服务器的所有网卡的MAC地址
(3)服务器上ip addr show
------------------------------------------------------------
面试题:
如何查看Linux服务器的网卡的MAC地址(Jesse)
(1)、ifconfig -a
[root@ohosts ~]# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:0C:29:3A:54:4C
inet addr:192.168.1.201 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe3a:544c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7510 errors:0 dropped:0 overruns:0 frame:0
TX packets:2392 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:641376 (626.3 KiB) TXbytes:245240 (239.4 KiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:3A:54:4C
inet addr:192.168.1.204 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:3A:54:4C
inet addr:192.168.1.203 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth1 Link encap:Ethernet HWaddr00:0C:29:3A:54:56
inet addr:172.16.1.7 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe3a:5456/64 Scope:Link
UP BROADCAST RUNNINGMULTICAST MTU:1500 Metric:1
RX packets:4973 errors:0 dropped:0 overruns:0 frame:0
TX packets:59 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:400784 (391.3 KiB) TXbytes:2694 (2.6 KiB)
eth2 Link encap:Ethernet HWaddr00:0C:29:3A:54:60 (Jesse)
inet addr:10.0.10.7 Bcast:10.0.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe3a:5460/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4951 errors:0 dropped:0 overruns:0 frame:0
TX packets:18538 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:396520 (387.2 KiB) TXbytes:4738427 (4.5 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:26 errors:0 dropped:0 overruns:0 frame:0
TX packets:26 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2104 (2.0 KiB) TXbytes:2104 (2.0 KiB)
sit0 Link encap:IPv6-in-IPv4
NOARP MTU:1480 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0(0.0 b)
[root@ohosts ~]#
(2)、ip link show
[root@ohosts ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether00:0c:29:3a:54:4c brd ff:ff:ff:ff:ff:ff
3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:3a:54:56 brd ff:ff:ff:ff:ff:ff
4: eth2:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:3a:54:60 brd ff:ff:ff:ff:ff:ff
5: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
(3)ip add show
[root@ohosts ~]# ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:3a:54:4c brdff:ff:ff:ff:ff:ff
inet 192.168.1.201/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.204/24 brd 192.168.1.255 scope global secondary eth0:0
inet 192.168.1.203/24 brd 192.168.1.255 scope global secondary eth0:1
inet6 fe80::20c:29ff:fe3a:544c/64 scope link
valid_lft forever preferred_lft forever
3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:3a:54:56 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.7/24 brd 172.16.1.255 scope global eth1
inet6 fe80::20c:29ff:fe3a:5456/64 scope link
valid_lft forever preferred_lft forever
4: eth2:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:3a:54:60 brd ff:ff:ff:ff:ff:ff
inet 10.0.10.7/24 brd 10.0.10.255 scope global eth2
inet6 fe80::20c:29ff:fe3a:5460/64 scope link
valid_lft forever preferred_lft forever
5: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
[root@ohosts ~]#
(4)[root@ohosts ~]# cat /proc/net/arp #仅是查看远端主机的MAC地址和IP地址,可以配合交换机查看其端口
#不能确定IP地址是哪个网卡,需要执行其他命令配合检查
IP address HW type Flags HW address Mask Device
192.168.1.1 0x1 0x2 F4:EC:38:78:E0:12 * eth0
192.168.1.250 0x1 0x2 9C:4E:36:96:FE:40 * eth2
192.168.1.250 0x1 0x2 9C:4E:36:96:FE:40 * eth0
192.168.1.250 0x1 0x2 9C:4E:36:96:FE:40 * eth1
[root@ohosts ~]#
(5)通过脚本直接获取MAC地址列表(Jesse)
for i in 0 1 2;do echo eth$i;cat/sys/class/net/eth$i/address;done
eth0
00:0c:29:3a:54:4c
eth1
00:0c:29:3a:54:56
eth2
00:0c:29:3a:54:60
(6)通过脚本直接获取本机的MAC地址列表
[root@picdata-1-1 ~]# ifconfig -a|grep HWaddr| awk '{print $1,$5}'
eth0 00:0C:29:3A:54:4C
eth0:0 00:0C:29:3A:54:4C
eth0:1 00:0C:29:3A:54:4C
eth1 00:0C:29:3A:54:56
eth2 00:0C:29:3A:54:60
(7)编程来实现
比较简单的方法是利用套接口(socket)和IO接口(ioctl)函数来获取网卡信息,需要引用如下文件:
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/if_ether.h>
#include <net/if.h>
#include <linux/sockios.h>
socket函数的原型是:(Jesse)
int socket(int domain,int type,int protocol);
本函数有以下3个输入参数:
domain参数:表示所使用的协议族;
type参数:表示套接口的类型;
protocol参数:表示所使用的协议族中某个特定的协议。
如果函数调用成功,套接口的描述符(非负整数)就作为函数的返回值,假如返回值为-1,就表明有错误发生。
利用socket函数来获取网卡MAC信息时,domain参数取值AF_INET,表示采用internet协议族;type参数指定为SOCK_DGRAM,表示采用数据报类型套接口,protocol参数在这种组合下只有唯一选择,故用0填充。
I/O控制函数ioctl用于对文件进行底层控制,这里的文件包含网卡、终端、磁带机、套接口等软硬件设施,实际的操作来自各个设备自己提供的ioctl接口。ioctl函数的原型如下:
int ioctl(int d,int request,…)
这里,参数d取值套接口的描述符,第一个request参数指定通过socket传输的I/O类型。本实验可以取值 SIONGIFHWADDR(0x8927),表示取硬件地址。其他取值及其含义详见/usr/includr/linux/sockios.h。其后的 request参数用于为实现I/O控制所必须传入或传出的参数。本实验需要用ifr结构传入网卡设备名,并传出6B的MAC地址。
下面示例获取本机MAC地址并打印出来:
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/if_ether.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <stdio.h>
#include <string.h>
void main(void)
{
char *device="eth0"; //teh0是网卡设备名
unsigned char macaddr[ETH_ALEN]; //ETH_ALEN(6)是MAC地址长度
struct ifreq req;
int err,i;
int s=socket(AF_INET,SOCK_DGRAM,0); //internet协议族的数据报类型套接口
strcpy(req.ifr_name,device); //将设备名作为输入参数传入
err=ioctl(s,SIOCGIFHWADDR,&req); //执行取MAC地址操作
close(s);
if(err != -1)
{
memcpy(macaddr,req.ifr_hwaddr.sa_data,ETH_ALEN); //取输出的MAC地址
for(i=0;i<ETH_ALEN;i++)
printf("=:,macaddr[i]);
}
}