A. 请教linux 协议栈内如何获取本机MAC地址
用 dev_get_by_name(&init_net,"br0") 可以获取到net_device结构体
里面的 dev_addr 参数就是MAC地址
B. linux查mac地址命令
方法1:ifconfig命令查看网卡MAC地址
/sbin/ifconfig | grep HWaddr 或 /sbin/ifconfig | grep ether
有些Linux发行版本的MAC地址字段为HWaddr,有些Linux发行版本的MAC地址字段为ether。根据实际情况选择上面命令。《Linux就该这么学》
方法2:/sys/class/net/xxx/address查看
根据网卡名调整,例如这里网卡名为ens160,如果网卡名为xxx,那么应该用/sys/class/net/xxx/address
方法3:ip命令查看网卡MAC地址
方法4:nmcli命令查看网卡MAC地址
方法5:dmesg命令查看网卡MAC地址
C. 如何获取linux系统的mac地址
①命令ifconfig -a 其中 HWaddr字段就是MAC地址
②或者使用grep过滤只显示MAC地址:
ifconfig-a|grep-ihw
#只输出当前电脑上所有网卡的mac地址(不显示IP等信息)
#eth0Linkencap:EthernetHWaddr******----这是有线网卡的MAC地址
#wlan0Linkencap:EthernetHWaddr******----这是无线网卡的MAC地址
D. 在局域网里面linux 网卡MAC地址获取
Linux系统有一个命令是tcpmp,它也可以实现抓包的功能。如果你知道这台主机的ip地址,很简单,通过tcpmp命令:两个终端窗口,一个用来ping,一个用来抓包,你可以分析出它的mac地址;如果什么都不知道的可能会有点麻烦,你需要做的事情很多,抓去数据包,同样你还要分析。
注,两条命令:
#tcpmp host 192.168.1.254 -vvv
host 192.168.1.254表示你要抓取数据包的主机,假如你要得到它的mac地址,再开一个窗口,通过ping 192.168.1.254,抓取的数据包中你就可以看到它的mac地址了。
E. linux建立TCP服务器后,TCP客户端与服务器连接成功后,怎样获取客户端的MAC地址
我认为你是从 socket中取不到这部分的信息的.
你得再操作 arp的缓存部分才行. ARP那部分如果在特殊情况,很乱套.比如有攻击或是IP地址设置有冲突啥地.
---
详细的C语言怎么操作ARP缓存我不太清楚. 但你如果想知道.就得查一下这部分怎么做了.
高层的socket操作是得不到的.
只有直连网段的计算机有MAC地址信息.经过路由来的数据包.取不到.
F. 有谁知道linux系统环境下,怎样在后台才能获取到mac地址
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 与系统相关的一些常用工具方法.
*
* @author stephen
* @version 1.0.0
*/
public class SystemTool {
/**
* 获取当前操作系统名称.
* return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}
/**
* 获取unix网卡的mac地址.
* 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
if (index >= 0) {// 找到了
mac = line.substring(index +"hwaddr".length()+ 1).trim();// 取出mac地址并去除2边空格
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* 获取widnows网卡的mac地址.
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]
if (index >= 0) {// 找到了
index = line.indexOf(":");// 寻找":"的位置
if (index>=0) {
mac = line.substring(index + 1).trim();// 取出mac地址并去除2边空格
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* 测试用的main方法.
*
* @param argc
* 运行参数.
*/
public static void main(String[] argc) {
String os = getOSName();
System.out.println(os);
if(os.startsWith("windows")){
//本地是windows
String mac = getWindowsMACAddress();
System.out.println(mac);
}else{
//本地是非windows系统 一般就是unix
String mac = getUnixMACAddress();
System.out.println(mac);
}
}
}
-------------------------------------------------------------------------
本程序可以正确获得本机IP地址和网卡"eth0"的MAC地址,已经在windowsXP和ubuntu-Linux上测试过
(注意:如果有多块网卡,可能出错)
下面给出代码:
import java.net.*;import java.util.*;
public class Test { public static void main(String[] args) { Test t = new Test(); System.out.println(t.getLocalIP()); System.out.println(t.getMacAddr()); }
public String getMacAddr() { String MacAddr = ""; String str = ""; try { NetworkInterface NIC = NetworkInterface.getByName("eth0"); byte[] buf = NIC.getHardwareAddress(); for (int i = 0; i < buf.length; i++) { str = str + byteHEX(buf[i]); } MacAddr = str.toUpperCase(); } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return MacAddr; }
public String getLocalIP() { String ip = ""; try { Enumeration<?> e1 = (Enumeration<?>) NetworkInterface .getNetworkInterfaces(); while (e1.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e1.nextElement(); if (!ni.getName().equals("eth0")) { continue; } else { Enumeration<?> e2 = ni.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress ia = (InetAddress) e2.nextElement(); if (ia instanceof Inet6Address) continue; ip = ia.getHostAddress(); } break; } } } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return ip; }
/* 一个将字节转化为十六进制ASSIC码的函数 */ public static String byteHEX(byte ib) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char[] ob = new char[2]; ob[0] = Digit[(ib >>> 4) & 0X0F]; ob[1] = Digit[ib & 0X0F]; String s = new String(ob); return s; }}
G. 在linux用java根据ip获得mac地址
调linux命令:arp <ip地址>
返回字符串中截取mac地址
H. 在linux系统中怎样查看mac地址
1、 首先在桌面右键选择“打开终端”,或者按ctrl + Alt + T快捷键打开终端
I. linux中怎么获取本机的mac地址
如果你是要修改linux的mac地址这很简单 再/etc/sysconfig/network-scripts/ifcfg-ethX (X是表示你的哪一张网卡) 修改其中的mac,修改成服务器能通过的mac就可以了,修改后,重启下网卡,为了将缓存中的mac地址修改
J. 在Linux系统下用Java语言获取客户端的IP地址,MAC地址,客户端的主机名称
这个网上很多,主要是机器必须支持ICMP和NETBIOS协议。你参考一下:
public String getIP()
{
InetAddress inet;
try {
inet =
InetAddress.getLocalHost();
InetAddress.getByName("");
return
inet.getHostAddress();
} catch (UnknownHostException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
return "";
}