发新话题
打印

怎样用JSP获取当前用户的网卡IP和MAC地址?

怎样用JSP获取当前用户的网卡IP和MAC地址?

服务器是linux
想用一个页面获取当前用户的网卡和MAC地址。
在网上找到了这个代码。
----------------------------------------------------------------------
获取本地主机的MAC地址
/*
FileName:MACHomework.java
Author:流浪小子
Date:2004-7-5
E-mail:qiyadeng@hotmail.com
Purpose:获取本地主机的MAC地址
*/
import java.io.*;
import java.util.*;




public class MACHomework
{
static private final int MACLength=18;
public static void main(String args[])
{
System.out.print ("本机的物理地址是:");
System.out.println(getMACAddress());
}
static public String getMACAddress()
{
SysCommand syscmd=new SysCommand();
//系统命令
String cmd="cmd.exe /c ipconfig/all";
Vector result;
result=syscmd.execute(cmd);
return getCmdStr(result.toString());
}
static public String  getCmdStr(String outstr)
{
String find="hysical Address. . . . . . . . . :";
int findIndex=outstr.indexOf(find);
if(findIndex==-1)
{
  return "未知错误!";
}
else
{
  return outstr.substring(findIndex+find.length()+1,findIndex+find.length()+MACLength);
}
}
}

//SysCommand类
class SysCommand
{
Process p;
public Vector execute(String cmd)
{
try
{
  Start(cmd);
  Vector  vResult=new Vector();
  DataInputStream in=new DataInputStream(p.getInputStream());
  BufferedReader myReader=new BufferedReader(new InputStreamReader(in));
  String line;
  do
  {
   line=myReader.readLine();
   if(line==null)
   {
    break;
   }
   else
   {
    vResult.addElement(line);
   }
  }while(true);
  myReader.close();
  return vResult;
}
catch(Exception e)
{
  return null;
  
}

}
public void Start(String cmd)
{
try
{
  if(p!=null)
  {
   kill();
  }
  Runtime sys=Runtime.getRuntime();
  p=sys.exec(cmd);
  
}
catch(Exception e)
{
  
}
}
public void kill()
{
if(p!=null)
{
  p.destroy ();
  p=null;
}
}

}
----------------------------------------------------------
这段代码
在WIN2000下运行通过。
在LINUX下运行提示:

java.lang.NullPointerException
at net.agent.MACHomework.getMACAddress(MACHomework.java:27)

是空指针,是不是因为这句话的问题?
在LINUX系统中没有此命令,所以才出现了空指针?

//系统命令
String cmd="cmd.exe /c ipconfig/all"; 请各位朋友帮忙,解决此问题?先谢了。      

TOP

above code uses system specified command to get network info, ipconfig for windows, ifconfig for linux. this's not a good solution. u can install pcap library (for both windows and linux), then use jpcap to get info from pcap       

TOP

我是想获取客户端用户的MAC地址,如果用了IFCONFIG,客户端能通过吗?      

TOP

Get client's MAC?! No hope, normal user under linux can't run 'ifconfig'.
But we can read "proc" filesystem, try "cat /proc/net/arp".      

TOP

cat /proc/net/arp
这个位置是网关的MAC地址,而我要获取的是客户端用户机器的MAC地址,这个就真的没希望吗?
为何WIN系统下就可以实现呢?      

TOP

The code you provide is just a normal standalone java application. It can only get its host's mac, no others'. JSP is Java Server Page, it runs on server side, not client side. I don't know how you can get client side's mac by JSP(Servlet) without a tool(pcap) of IP layer. Of course, if it's possible, you may use javascript to finish the work. JavaScript runs in client side's browser. But most browsers limit its power.

By the way, if any web page can get your mac without your permission, do you feel www is horrible?      

TOP

发新话题