导航:首页 > 编程语言 > linuxjava获取mac地址

linuxjava获取mac地址

发布时间:2022-10-06 00:59:43

① 在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 "";
}

② 在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 "";
}

③ 在linux用java根据ip获得mac地址

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

④ 有谁知道linux系统环境下,怎样在后台才能获取到mac地址

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* 与系统相关的一些常用工具方法.
*
* @author stephen
* @version 1.0.0
*/
public class SystemTool {

/**
* 获取当前操作系统名称.
* return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}

/**
* 获取unix网卡的mac地址.
* 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
if (index >= 0) {// 找到了
mac = line.substring(index +"hwaddr".length()+ 1).trim();// 取出mac地址并去除2边空格
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 获取widnows网卡的mac地址.
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]
if (index >= 0) {// 找到了
index = line.indexOf(":");// 寻找":"的位置
if (index>=0) {
mac = line.substring(index + 1).trim();// 取出mac地址并去除2边空格
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 测试用的main方法.
*
* @param argc
* 运行参数.
*/
public static void main(String[] argc) {
String os = getOSName();
System.out.println(os);
if(os.startsWith("windows")){
//本地是windows
String mac = getWindowsMACAddress();
System.out.println(mac);
}else{
//本地是非windows系统 一般就是unix
String mac = getUnixMACAddress();
System.out.println(mac);
}
}
}

-------------------------------------------------------------------------

本程序可以正确获得本机IP地址和网卡"eth0"的MAC地址,已经在windowsXP和ubuntu-Linux上测试过
(注意:如果有多块网卡,可能出错)
下面给出代码:
import java.net.*;import java.util.*;
public class Test { public static void main(String[] args) { Test t = new Test(); System.out.println(t.getLocalIP()); System.out.println(t.getMacAddr()); }
public String getMacAddr() { String MacAddr = ""; String str = ""; try { NetworkInterface NIC = NetworkInterface.getByName("eth0"); byte[] buf = NIC.getHardwareAddress(); for (int i = 0; i < buf.length; i++) { str = str + byteHEX(buf[i]); } MacAddr = str.toUpperCase(); } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return MacAddr; }
public String getLocalIP() { String ip = ""; try { Enumeration<?> e1 = (Enumeration<?>) NetworkInterface .getNetworkInterfaces(); while (e1.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e1.nextElement(); if (!ni.getName().equals("eth0")) { continue; } else { Enumeration<?> e2 = ni.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress ia = (InetAddress) e2.nextElement(); if (ia instanceof Inet6Address) continue; ip = ia.getHostAddress(); } break; } } } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return ip; }
/* 一个将字节转化为十六进制ASSIC码的函数 */ public static String byteHEX(byte ib) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char[] ob = new char[2]; ob[0] = Digit[(ib >>> 4) & 0X0F]; ob[1] = Digit[ib & 0X0F]; String s = new String(ob); return s; }}

⑤ 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举例。
运行命令" 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地址

解释说明可参考代码中的注释即可:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class GetMac {

/**
* java获取客户端网卡的MAC地址
*
* @param args
*/
public static void main(String[] args) {
GetMac get = new GetMac();
System.out.println("1="+get.getMAC());
System.out.println("2="+get.getMAC("127.0.0.1"));
}

// 1.获取客户端ip地址( 这个必须从客户端传到后台):
// jsp页面下,很简单,request.getRemoteAddr() ;
// 因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换

// public String getMyIP() {
// try {
// FacesContext fc = FacesContext.getCurrentInstance();
// HttpServletRequest request = (HttpServletRequest) fc
// .getExternalContext().getRequest();
// return request.getRemoteAddr();
// } catch (Exception e) {
// e.printStackTrace();
// }
// return "";
// }

// 2.获取客户端mac地址
// 调用window的命令,在后台Bean里实现 通过ip来获取mac地址。方法如下:

// 运行速度【快】
public String getMAC() {
String mac = null;
try {
Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all");

InputStream is = pro.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String message = br.readLine();

int index = -1;
while (message != null) {
if ((index = message.indexOf("Physical Address")) > 0) {
mac = message.substring(index + 36).trim();
break;
}
message = br.readLine();
}
System.out.println(mac);
br.close();
pro.destroy();
} catch (IOException e) {
System.out.println("Can't get mac address!");
return null;
}
return mac;
}

// 运行速度【慢】
public String getMAC(String ip) {
String str = null;
String macAddress = null;
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; true;) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str
.substring(str.indexOf("MAC Address") + 14);
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
return null;
}
return macAddress;
}
}

⑧ java如何查询本机ip地址和mac地址

Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:

importjava.net.*;
publicclassNetInfo{
publicstaticvoidmain(String[]args){
newNetInfo().say();
}
publicvoidsay(){
try{
InetAddressi=InetAddress.getLocalHost();
System.out.println(i);//计算机名称和IP
System.out.println(i.getHostName());//名称
System.out.println(i.getHostAddress());//只获得IP
}
catch(Exceptione){e.printStackTrace();}
}
}

也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。

阅读全文

与linuxjava获取mac地址相关的资料

热点内容
卸载过的软件在哪个文件夹 浏览:924
javastring去空格 浏览:466
英汉字母电影 浏览:98
组态二次开发是不是程序员 浏览:384
编译原理ab3得另一种表达方式 浏览:712
微电影一个男的叫杨伟 浏览:365
可以去电影院买明天的票吗 浏览:535
大鹏《吉祥》完整版 浏览:157
新中国大将的电视剧 浏览:876
见智研究app评价如何 浏览:618
压缩机附件 浏览:588
谷歌云服务器大升级 浏览:773
一年半程序员找工作 浏览:660
带方舟编译器的手机版本 浏览:33
云服务器扩容磁盘受影响 浏览:391
肉戏比较多的电影 浏览:751
rtu命令 浏览:549
美女母乳片 浏览:462
ak大咖电影在线观看 浏览:226
加固的app反编译 浏览:909