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

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

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 = 0i <buf.lengthi++) {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}}

有三种方法:方法一:1.关闭网卡设备ifconfig eth0 down2.修改MAC地址ifconfig eth0 hw ether MAC地址3.重启网卡ifconfig eth0 up方法二:以上方法一修改后linux重启后MAC又恢复为原来的,为了下次启动时修改后的MAC仍有效,我们可以修改文件file:/etc/rc.d/rc.sysinit(RedFlag Linux为这个文件,其他版本的linux应该不同)的内容,在该文件末尾加以下内容:ifconfig eth0 downifconfig eth0 hw ether MAC地址ifconfig eth0 up方法三:很简单的,只是在./etc/sysconfig/network-scripts/ifcfg-eth0中加入下面一句话: MACADDR=00:AA:BB:CC:DD:EE

可以使用ifconfig命令。ifconfig是linux中用于显示或配置网络设备(网络接口卡)的命令,英文全称是network interfaces configuring。它能够显示网卡的IP地址、子网掩码、广播地址、硬件地址等信息。

用法示例:

查看网卡eth0的mac地址

$ ifconfig eth0

mac地址位于上图中的红色方框处。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8694505.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-20
下一篇2023-04-20

发表评论

登录后才能评论

评论列表(0条)

    保存