導航:首頁 > 編程語言 > linuxjava獲取mac地址

linuxjava獲取mac地址

發布時間:2022-10-06 00:59:43

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

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

③ 在linux用java根據ip獲得mac地址

調linux命令:arp <ip地址>
返回字元串中截取mac地址

④ 有誰知道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; }}

⑤ java怎麼獲取系統mac地址

首先,創建工程,包,和一個類。
在此不加詳述,我們直接看代碼。
這里,我把這個類命名為GetMacAddr
這里,最最關鍵的就是這里這個方法。
我們通過NetworkInterface這個類來操作。
也就是通過getLocalHost()方法先得到本機IP,
然後調用getHardwareAddress()方法得到一個byte數組的地址。
我們把六位地址傳到一個byte數組裡面,然後輸出來就是。
不多廢話,看代碼:
private void getMACAddr()
throws SocketException, UnknownHostException {
// 獲得IP
NetworkInterface netInterface =
NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
// 獲得Mac地址的byte數組
byte[] macAddr = netInterface.getHardwareAddress();
System.out.print("MAC Addr:\t");
// 循環輸出
for (byte b : macAddr) {
// 這里的toHexString()是自己寫的格式化輸出的方法,見下步。
System.out.print(toHexString(b) + " ");
}
}
上一步驟中,為什麼會出現一個toHexString()方法呢?
因為可能10進制轉16進制時候可能會出現單字元,
所以,如果有出現單字元的情況,我們在其前面添加一個「0」做佔位符。
這也是為了視覺的直觀,也夾帶著個人的習慣。
private static String toHexString(int integer) {
// 將得來的int類型數字轉化為十六進制數
String str = Integer.toHexString((int) (integer & 0xff));
// 如果遇到單字元,前置0佔位補滿兩格
if (str.length() == 1) {
str = "0" + str;
}
return str;
}

⑥ java如何獲取mac地址

以windows舉例。
運行命令" cmd ipconfig /all"就會出現以下結果

Physical Address. . . . . . . . . : 20-CF-30-9A-60-EE

java就能過這樣的命令來獲取。以下是示例。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestMac
{
public static void main(String[] args) {
System.out.println("Operation System=" + getOsName());
System.out.println("Mac Address=" + getMACAddress());
System.out.println("通過ip獲取mac"+getMACAddress("192.168.1.101"));
}

public static String getOsName() {
String os = "";
os = System.getProperty("os.name");
return os;
}

public static String getMACAddress() {
String address = "";
String os = getOsName();
if (os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") > 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
}
}
address = address.trim();
return address;
}

public static String getMACAddress(String ipAddress) {
String str = "", strMAC = "", macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
}
//
if (strMAC.length() < 17) {
return "Error!";
}

macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
+ ":" + strMAC.substring(12, 14) + ":"
+ strMAC.substring(15, 17);
//
return macAddress;
}
}

劍天夢的回答原理和我這個一樣,都是通過Process 執行命令。 我直接補充到答案里了。不過
我這邊運行那個命令出來的結果很多,那麼花的時間就長了。優點是能夠獲取別人的mac地址 。

⑦ java如何獲取mac地址

解釋說明可參考代碼中的注釋即可:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class GetMac {

/**
* java獲取客戶端網卡的MAC地址
*
* @param args
*/
public static void main(String[] args) {
GetMac get = new GetMac();
System.out.println("1="+get.getMAC());
System.out.println("2="+get.getMAC("127.0.0.1"));
}

// 1.獲取客戶端ip地址( 這個必須從客戶端傳到後台):
// jsp頁面下,很簡單,request.getRemoteAddr() ;
// 因為系統的VIew層是用JSF來實現的,因此頁面上沒法直接獲得類似request,在bean里做了個強制轉換

// public String getMyIP() {
// try {
// FacesContext fc = FacesContext.getCurrentInstance();
// HttpServletRequest request = (HttpServletRequest) fc
// .getExternalContext().getRequest();
// return request.getRemoteAddr();
// } catch (Exception e) {
// e.printStackTrace();
// }
// return "";
// }

// 2.獲取客戶端mac地址
// 調用window的命令,在後台Bean里實現 通過ip來獲取mac地址。方法如下:

// 運行速度【快】
public String getMAC() {
String mac = null;
try {
Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all");

InputStream is = pro.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String message = br.readLine();

int index = -1;
while (message != null) {
if ((index = message.indexOf("Physical Address")) > 0) {
mac = message.substring(index + 36).trim();
break;
}
message = br.readLine();
}
System.out.println(mac);
br.close();
pro.destroy();
} catch (IOException e) {
System.out.println("Can't get mac address!");
return null;
}
return mac;
}

// 運行速度【慢】
public String getMAC(String ip) {
String str = null;
String macAddress = null;
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; true;) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str
.substring(str.indexOf("MAC Address") + 14);
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
return null;
}
return macAddress;
}
}

⑧ java如何查詢本機ip地址和mac地址

Java中可以使用程序來獲取本地ip地址和mac地址,使用InetAddress這個工具類,示例如下:

importjava.net.*;
publicclassNetInfo{
publicstaticvoidmain(String[]args){
newNetInfo().say();
}
publicvoidsay(){
try{
InetAddressi=InetAddress.getLocalHost();
System.out.println(i);//計算機名稱和IP
System.out.println(i.getHostName());//名稱
System.out.println(i.getHostAddress());//只獲得IP
}
catch(Exceptione){e.printStackTrace();}
}
}

也可以通過命令行窗口來查看本地ip和mac地址,輸入命令:ipconfig。

閱讀全文

與linuxjava獲取mac地址相關的資料

熱點內容
壓縮機lj100cy 瀏覽:556
王者系統怎麼轉回安卓系統 瀏覽:749
linux查看路由表命令 瀏覽:506
高手程序員使用什麼筆記本 瀏覽:440
ios壓縮圖片app 瀏覽:839
排隊論pdf 瀏覽:520
python調用無參函數 瀏覽:799
主管開除女程序員 瀏覽:713
雲伺服器轉售 瀏覽:541
壓縮空氣漏氣量怎樣計算 瀏覽:103
手機app是怎麼跳轉的 瀏覽:664
學編程的重要性 瀏覽:25
程序員去按摩 瀏覽:740
奧迪手機控車app怎麼添加愛車 瀏覽:5
收磚機石獅編程培訓廠家 瀏覽:762
吉里吉里2安卓模擬器怎麼用 瀏覽:819
編譯器將匯編代碼 瀏覽:682
電路板加密錯誤 瀏覽:21
java自動機 瀏覽:364
相機連拍解壓 瀏覽:32