導航:首頁 > 編程語言 > java獲取ipmac地址

java獲取ipmac地址

發布時間:2023-08-20 07:23:58

java如何獲取區域網內所有安卓設備的ip地址,MAC以及序列號

1.得到區域網網段,可由自己機器的IP來確定 (也可以手動獲取主機IP-CMD-ipconfig /all)
2.根據IP類型,一次遍歷區域網內IP地址
JAVA類,編譯之後直接運行便可以得到區域網內所有IP,具體怎樣使用你自己編寫相應代碼調用便可
代碼如下::
package bean;

import java.io.*;
import java.util.*;

public class Ip{
static public HashMap ping; //ping 後的結果集
public HashMap getPing(){ //用來得到ping後的結果集
return ping;
}

//當前線程的數量, 防止過多線程摧毀電腦
static int threadCount = 0;

public Ip() {
ping = new HashMap();
}

public void Ping(String ip) throws Exception{
//最多30個線程
while(threadCount>30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}

public void PingAll() throws Exception{
//首先得到本機的IP,得到網段
InetAddress host = InetAddress.getLocalHost();
String hostAddress = host.getHostAddress();
int k=0;
k=hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0,k+1);
for(int i=1;i <=255;i++){ //對所有區域網Ip
String iip=ss+i;
Ping(iip);
}

//等著所有Ping結束
while(threadCount>0)
Thread.sleep(50);
}

public static void main(String[] args) throws Exception{
Ip ip= new Ip();
ip.PingAll();
java.util.Set entries = ping.entrySet();
Iterator iter=entries.iterator();

String k;
while(iter.hasNext()){
Map.Entry entry=(Map.Entry)iter.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();

if(value.equals("true"))
System.out.println(key+"-->"+value);
}
}
class PingIp extends Thread{
public String ip; // IP
public PingIp(String ip){
this.ip=ip;
}

public void run(){
try{
Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
//讀取結果行
for (int i=1 ; i <7; i++)
input.readLine();
String line= input.readLine();

if (line.length() <17 || line.substring(8,17).equals("timed out"))
ping.put(ip,"false");
else
ping.put(ip,"true");
//線程結束
threadCount -= 1;
}catch (IOException e){}
}
}
}

Ⅱ 求獲取客戶端mac地址java代碼,急需急需,麻煩了

通過ip獲取指定ip地址的mac地址,ip可以通過請求request獲取,
request.getRemoteAddr();
(當然獲取ip也不是在任何情況下都有效的)
通過了Apache,Squid等反向代理軟體就不能獲取到客戶端的真實IP地址。
您也許需要通過其他的方式獲取,(見附)
//獲取mac如下 (nbtstat -A IPAddress是對給定的IP地址解析其主機名。如果不能正常解析它的主機
//名的話,有可能是防火牆屏蔽了。也可能是在DNS中將NetBios 解析選項屏蔽了。)
public String getMACAddress(String ip){
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.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) {
macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
break;
}
if (str.indexOf("MAC Address") > 1) {
macAddress = str.substring(str.indexOf("MAC 地址") + 14, str.length());
break;
}
//以上有個判斷,不同系統cmd命令執行的返回結果展示方式不一樣,我測試的win7是MAC 地址
//所以又第二個if判斷 你可先在你機器上cmd測試下nbtstat -A 命令 當然得有一個你可以ping通的
//網路ip地址,然後根據你得到的結果中mac地址顯示方式來確定這個循環取值

}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
附:
通過代理了的客戶端ip地址獲取方式
於是可得出獲得客戶端真實IP地址的方法一:
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}

獲得客戶端真實IP地址的方法二:
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}

可是,如果通過了多級反向代理的話,X-Forwarded-For的值並不止一個,而是一串IP值,究竟哪個才是真正的用戶端的真實IP呢?

答案是取X-Forwarded-For中第一個非unknown的有效IP字元串。如:
X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100
用戶真實IP為: 192.168.1.110

Ⅲ 請問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地址

首先,創建工程,包,和一個類。
在此不加詳述,我們直接看代碼。
這里,我把這個類命名為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舉例。x0dx0a運行命令" cmd ipconfig /all"就會出現以下結果x0dx0a x0dx0aPhysical Address. . . . . . . . . : 20-CF-30-9A-60-EEx0dx0a。x0dx0ajava就能過這樣的命令來獲取。以下是示例。x0dx0ax0dx0aimport java.io.BufferedReader;x0dx0aimport java.io.IOException;x0dx0aimport java.io.InputStreamReader;x0dx0ax0dx0apublic class TestMacx0dx0a{x0dx0a public static void main(String[] args) {x0dx0a System.out.println("Operation System=" + getOsName());x0dx0a System.out.println("Mac Address=" + getMACAddress());x0dx0a System.out.println("通過ip獲取mac"+getMACAddress("192.168.1.101"));x0dx0a }x0dx0ax0dx0a public static String getOsName() {x0dx0a String os = "";x0dx0a os = System.getProperty("os.name");x0dx0a return os;x0dx0a }x0dx0a x0dx0a public static String getMACAddress() {x0dx0a String address = "";x0dx0a String os = getOsName();x0dx0a if (os.startsWith("Windows")) {x0dx0a try {x0dx0a String command = "cmd.exe /c ipconfig /all";x0dx0a Process p = Runtime.getRuntime().exec(command);x0dx0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));x0dx0a String line;x0dx0a while ((line = br.readLine()) != null) {x0dx0a if (line.indexOf("Physical Address") > 0) {x0dx0a int index = line.indexOf(":");x0dx0a index += 2;x0dx0a address = line.substring(index);x0dx0a break;x0dx0a }x0dx0a }x0dx0a br.close();x0dx0a return address.trim();x0dx0a } catch (IOException e) {x0dx0a }x0dx0a } else if (os.startsWith("Linux")) {x0dx0a String command = "/bin/sh -c ifconfig -a";x0dx0a Process p;x0dx0a try {x0dx0a p = Runtime.getRuntime().exec(command);x0dx0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));x0dx0a String line;x0dx0a while ((line = br.readLine()) != null) {x0dx0a if (line.indexOf("HWaddr") > 0) {x0dx0a int index = line.indexOf("HWaddr") + "HWaddr".length();x0dx0a address = line.substring(index);x0dx0a break;x0dx0a }x0dx0a }x0dx0a br.close();x0dx0a } catch (IOException e) {x0dx0a }x0dx0a }x0dx0a address = address.trim();x0dx0a return address;x0dx0a }x0dx0ax0dx0apublic static String getMACAddress(String ipAddress) { x0dx0aString str = "", strMAC = "", macAddress = ""; x0dx0atry { x0dx0aProcess pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress); x0dx0aInputStreamReader ir = new InputStreamReader(pp.getInputStream()); x0dx0aLineNumberReader input = new LineNumberReader(ir); x0dx0afor (int i = 1; i < 100; i++) { x0dx0astr = input.readLine(); x0dx0aif (str != null) { x0dx0aif (str.indexOf("MAC Address") > 1) { x0dx0astrMAC = str.substring(str.indexOf("MAC Address") + 14, x0dx0astr.length()); x0dx0abreak; x0dx0a} x0dx0a} x0dx0a} x0dx0a} catch (IOException ex) { x0dx0areturn "Can't Get MAC Address!"; x0dx0a} x0dx0a// x0dx0aif (strMAC.length() < 17) { x0dx0areturn "Error!"; x0dx0a} x0dx0ax0dx0amacAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) x0dx0a+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) x0dx0a+ ":" + strMAC.substring(12, 14) + ":" x0dx0a+ strMAC.substring(15, 17); x0dx0a// x0dx0areturn macAddress; x0dx0a} x0dx0a} x0dx0ax0dx0a劍天夢的回答原理和我這個一樣,都是通過Process 執行命令。 我直接補充到答案里了。不過x0dx0a我這邊運行那個命令出來的結果很多,那麼花的時間就長了。優點是能夠獲取別人的mac地址 。

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

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

Ⅶ JAVA如何獲取客戶端IP地址和MAC地址

/**
* 獲取IP地址
* @return
*/
public static String GetNetIp() {
URL infoUrl = null;
InputStream inStream = null;
String line = "";
try {
infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inStream = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
// 從反饋的結果中提取出IP地址
int start = strber.indexOf("{");
int end = strber.indexOf("}");
String json = strber.substring(start, end + 1);
if (json != null) {
try {
JSONObject jsonObject = new JSONObject(json);
line = jsonObject.optString("cip");
} catch (JSONException e) {
e.printStackTrace();
}
}
return line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}

public static String getLocalMacAddress() {//沒有緩存的地址,則查詢
String mac_s = ""; try { byte[] mac;
NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
mac = ne.getHardwareAddress();
mac_s = byte2hex(mac);
} catch (Exception e) {
} mac_s; return mac_s;
}

Ⅷ java如何獲取客戶端mac地址

以下代碼實現瀏覽器中獲取mac地址放入一個輸入框或隱藏域,隨登錄信息一起提交到伺服器。因為安全級別的關系可能會出現警告,可選擇允許執行。長期使用的話建議用戶將改站點加入信任站點或把安全級別調低。

<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo>
document.forms[0].txtMACAddr.value=unescape(MACAddr);
document.forms[0].txtIPAddr.value=unescape(IPAddr);
document.forms[0].txtDNSName.value=unescape(sDNSName);
//document.formbar.submit();
</SCRIPT>

<SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)
{
if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
MACAddr = objObject.MACAddress;
if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
IPAddr = objObject.IPAddress(0);
if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>
<SCRIPT language=JScript>
var service = locator.ConnectServer();
var MACAddr ;
var IPAddr ;
var DomainAddr;
var sDNSName;
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</SCRIPT>

<FORM id="formfoo" name="formbar" action="index.do" method="post"><INPUT value="00-11-11-B4-52-EF" name="txtMACAddr"> <INPUT value="210.42.38.50" name="txtIPAddr"> <INPUT value="zhupan" name="txtDNSName"> </FORM></BODY></HTML>

閱讀全文

與java獲取ipmac地址相關的資料

熱點內容
android自動化環境 瀏覽:251
androidrealm加密 瀏覽:511
地圖正在解壓縮是什麼意思 瀏覽:215
電腦軟體能放在文件夾嗎 瀏覽:784
uc伺服器怎麼打開 瀏覽:363
net怎麼編譯 瀏覽:242
我的世界187伺服器地址ip 瀏覽:953
拍賣房價的演算法 瀏覽:438
linux內核編譯視頻教程 瀏覽:881
程序員厚黑 瀏覽:207
如何在閑魚淘二手安卓機 瀏覽:175
怎麼下載晨星app 瀏覽:132
兩台伺服器如何同步內容 瀏覽:808
伺服器共用一個ip有什麼壞處 瀏覽:461
go加密exe 瀏覽:606
pdf改分欄 瀏覽:123
python執行怎麼寫 瀏覽:766
遇見她app怎麼加好友 瀏覽:548
手機怎麼設置app強制提醒 瀏覽:77
怎樣不用海綿做解壓玩具 瀏覽:81