java获取ipmac
‘壹’ 求获取客户端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 获取本机Ip地址和对应的mac地址 大家看看那里不够好
楼主,代码只枚举了网卡,还没有枚举到IP地址,,,,,,一张网卡,可以有多个IP的。
‘叁’ java如何不使用HttpServletRequest获取电脑客户端ip地址与Mac地址。
import java.net.InetAddress;
import java.net.NetworkInterface;
/**
*@author:
*@version:
*@description:
*/
public class Ipconfig {
public static void main(String[] args) throws Exception {
try {
InetAddress ia=InetAddress.getLocalHost();
String localname=ia.getHostName();
String localip=ia.getHostAddress();
System.out.println("本机名称是:"+ localname);
System.out.println("本机的ip是 :"+localip);
System.out.println("MAC ......... "+getMACAddress(ia));
} catch (Exception e) {
e.printStackTrace();
}
}
//获取MAC地址的方法
private static String getMACAddress(InetAddress ia)throws Exception{
//获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
//下面代码是把mac地址拼装成String
StringBuffer sb = new StringBuffer();
for(int i=0;i<mac.length;i++){
if(i!=0){
sb.append("-");
}
//mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length()==1?0+s:s);
}
//把字符串所有小写字母改为大写成为正规的mac地址并返回
return sb.toString().toUpperCase();
}
}
‘肆’ java 怎样利用IP地址获得局域网计算机的名字、mac地址、工作组
指定IP的MAC 代码如下: Java code
System.out.println("192.168.1.187对应网卡的MAC是:");NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName("192.168.1.187"));byte[]mac=ne.getHardwareAddress();String mac_s=hexByte(mac[0])+":"+hexByte(mac[1])+":"+ hexByte(mac[2])+":"+hexByte(mac[3])+":"+ hexByte(mac[4])+":"+hexByte(mac[5]);System.out.println(mac_s); 程序运行结果: 192.168.1.187对应网卡的MAC是: 00:0c:f1:20:75:58
工作组和计算机名字类似,可以到库里找
‘伍’ JAVA中如何获取指定IP的MAC地址
public static String getMacAddressIP(String remotePcIP) {
String str = "";
String macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP);
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) {
macAddress = str.substring(
str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException ex) {
}
return macAddress;
}
‘陆’ 利用Java,怎样获得指定IP的对应的MAC地址
import java.net.*;
public class NetInfo {
public static void main(String[] args) {
new NetInfo().say();
}
public void say() {
try {
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); //计算机名称和ip
System.out.println(i.getHostName()); //名称
System.out.println(i.getHostAddress()); //只获得ip
}
catch(Exception e){e.printStackTrace();}
}
}
‘柒’ 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){}
}
}
}
‘捌’ 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;
}
‘玖’ 在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 "";
}