Ⅰ 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>