import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Dos {
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
Dos test =new Dos();
System.out.println(test.getIP());
System.out.println(test.getHostName());
System.out.println("mak="+test.getMACAddressByIp("10.66.81.63"));
}
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 "";
}
public String getHostName(){
InetAddress inet;
try {
inet = InetAddress.getLocalHost();
InetAddress.getByName("");
return inet.getHostName();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
//ip必须是真实的ip,如:10.66.204.44,而不能是127.0.0.1或localhsot,否则返回"".可以指定可ping通的所有计算机的IP。
public String getMACAddressByIp(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;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
}
/**
* 直接获取mac地址(何伟)
* @return
*/
private static String getMACAddress()
{
String address = "";
String os = System.getProperty("os.name");
if ((os != null) && (os.startsWith("Windows")))
try
{
String line;
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
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 localIOException) {
}
return address;
}
二、web项目获取服务器端IP及客户端IP
String LocalAddr = request.getLocalAddr();//127.0.0.1
String LocalName = request.getLocalName();//localhsot
String test2 = "getRemoteHost="+request.getRemoteHost()+";getRemotePort="+request.getRemotePort()+";getRemoteUser="+request.getRemoteUser()+";getRemoteAddr="+request.getRemoteAddr();
System.out.println(test2);
String RemoteAddr = request.getRemoteAddr();
System.out.println(getRemoteAddress(request));
request.getSession().setAttribute("LocalAddr", LocalAddr);
request.getSession().setAttribute("LocalName", LocalName);
request.getSession().setAttribute("test2", test2);
request.getSession().setAttribute("RemoteAddr", RemoteAddr);
request.getRequestDispatcher("test.jsp").forward(request, response);
分享到:
相关推荐
获取客户端ip地址( 这个必须从客户端传到后台): jsp页面下,很简单,request....因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换java 获取客户端ip mac地址
java中通过ip获取远程客户端的mac地址,支持跨平台
获取用户请求IP地址 根据IP地址获取mac地址
主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下
在Controller类获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了。如果使用了反向代理软件,用...
可以通过java获取客户端IPMAC主机名等信息。下载就能用,欢迎下载!
在对网内主机进行监控时需要通过java获取远程机器的mac地址
Java获取本机MAC地址IP地址主机名
java获取客户端ip及mac地址方法,适合web开发者
获取计算机的mac地址和ip地址,mac地址返回的是一个list,ip地址是string
java通过ip获取mac地址源代码,正在学习或研究java开发的朋友可以看看,主要实现了局域网的扫描,如果有多个IP,只匹配本IP对应的Mac
LINUX编程,通过IP地址获取MAC地址代码。方
自己写的一个小工具,方便查看本机的IP和MAC地址,如果是笔记本,可以采集到两个MAC地址。 采用的技术是Eclipse 4 RCP,算是练手。程序打包时绑定了JRE,解压后就可以运行。
java获取IP地址和MAC地址。 /** * 通过HttpServletRequest返回IP地址 * @param request HttpServletRequest * @return ip String * @throws Exception */ public String getIpAddr(HttpServletRequest ...
import java.net.Inet4Address; import java.net.InetAddress; import java.util.Arrays; import jpcap.*;...public static byte[] arp(InetAddress ip) throws java.io.IOException{ //发现本机器的网络接口
通过使用SNMP协议获取某个交换机或服务器相关设备信息,如获取交换机设备,则可以通过设备OID获取相关信息,以及能获取交换机下所有连接的PC机的MAC地址与IP地址以及端口号信息。
获取本地IP地址,获取本地Host名称,获取Linux下的IP地址,获取用户真实IP地址,get/post请求 相应的工具类
跨网段获取远程主机MAC地址的方法与java实现
Android获取手机ip地址及mac地址
主要为大家详细介绍了java根据本地IP获取mac地址的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下