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 "";
}