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

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

import javaioBufferedReader;

import javaioIOException;

import javaioInputStreamReader;

/

与系统相关的一些常用工具方法

@author stephen

@version 100

/

public class SystemTool {

/

获取当前 *** 作系统名称

return *** 作系统名称 例如:windows xp,linux 等

/

public static String getOSName() {

return SystemgetProperty("osname")toLowerCase();

}

/

获取unix网卡的mac地址

非windows的系统默认调用本方法获取如果有特殊系统请继续扩充新的取mac地址方法

@return mac地址

/

public static String getUnixMACAddress() {

String mac = null;

BufferedReader bufferedReader = null;

Process process = null;

try {

process = RuntimegetRuntime()exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息

bufferedReader = new BufferedReader(new InputStreamReader(process

getInputStream()));

String line = null;

int index = -1;

while ((line = bufferedReaderreadLine()) != null) {

index = linetoLowerCase()indexOf("hwaddr");// 寻找标示字符串[hwaddr]

if (index >= 0) {// 找到了

mac = linesubstring(index +"hwaddr"length()+ 1)trim();// 取出mac地址并去除2边空格

break;

}

}

} catch (IOException e) {

eprintStackTrace();

} finally {

try {

if (bufferedReader != null) {

bufferedReaderclose();

}

} catch (IOException e1) {

e1printStackTrace();

}

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 = RuntimegetRuntime()exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息

bufferedReader = new BufferedReader(new InputStreamReader(process

getInputStream()));

String line = null;

int index = -1;

while ((line = bufferedReaderreadLine()) != null) {

index = linetoLowerCase()indexOf("physical address");// 寻找标示字符串[physical address]

if (index >= 0) {// 找到了

index = lineindexOf(":");// 寻找":"的位置

if (index>=0) {

mac = linesubstring(index + 1)trim();// 取出mac地址并去除2边空格

}

break;

}

}

} catch (IOException e) {

eprintStackTrace();

} finally {

try {

if (bufferedReader != null) {

bufferedReaderclose();

}

} catch (IOException e1) {

e1printStackTrace();

}

bufferedReader = null;

process = null;

}

return mac;

}

/

测试用的main方法

@param argc

运行参数

/

public static void main(String[] argc) {

String os = getOSName();

Systemoutprintln(os);

if(osstartsWith("windows")){

//本地是windows

String mac = getWindowsMACAddress();

Systemoutprintln(mac);

}else{

//本地是非windows系统 一般就是unix

String mac = getUnixMACAddress();

Systemoutprintln(mac);

}

}

}

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

本程序可以正确获得本机IP地址和网卡"eth0"的MAC地址,已经在windowsXP和ubuntu-Linux上测试过

(注意:如果有多块网卡,可能出错)

下面给出代码:

import javanet;import javautil;

public class Test { public static void main(String[] args) { Test t = new Test(); Systemoutprintln(tgetLocalIP()); Systemoutprintln(tgetMacAddr()); }

public String getMacAddr() { String MacAddr = ""; String str = ""; try { NetworkInterface NIC = NetworkInterfacegetByName("eth0"); byte[] buf = NICgetHardwareAddress(); for (int i = 0; i < buflength; i++) { str = str + byteHEX(buf[i]); } MacAddr = strtoUpperCase(); } catch (SocketException e) { eprintStackTrace(); Systemexit(-1); } return MacAddr; }

public String getLocalIP() { String ip = ""; try { Enumeration<> e1 = (Enumeration<>) NetworkInterface getNetworkInterfaces(); while (e1hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e1nextElement(); if (!nigetName()equals("eth0")) { continue; } else { Enumeration<> e2 = nigetInetAddresses(); while (e2hasMoreElements()) { InetAddress ia = (InetAddress) e2nextElement(); if (ia instanceof Inet6Address) continue; ip = iagetHostAddress(); } break; } } } catch (SocketException e) { eprintStackTrace(); Systemexit(-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; }}

如果你是要修改linux的mac地址这很简单再/etc/sysconfig/network-scripts/ifcfg-ethX(X是表示你的哪一张网卡)修改其中的mac,修改成服务器能通过的mac就可以了,修改后,重启下网卡,为了将缓存中的mac地址修改

不同的 *** 作系统下,查看的命令稍有不同。在Windows *** 作系统下,首先进入MS-DOS(或命令 *** 作符)状态,然后输入查看MAC地址命令,即可获得适配器地址 (即MAC地址)。在 《开始菜单》《运行》中执行下面的命令

Linux/Unix *** 作系统熟悉的人都通常是用console命令控制台来进行相应的 *** 作。 Linux/Unix *** 作系统查看网卡mac地址的方法可以通过以下命令获得:

1ifconfig -a

2ip link show

3ip address show

如果你是要修改linux的mac地址这很简单 再/etc/sysconfig/network-scripts/ifcfg-ethX (X是表示你的哪一张网卡) 修改其中的mac,修改成服务器能通过的mac就可以了,修改后,重启下网卡,为了将缓存中的mac地址修改

以上就是关于有谁知道linux系统环境下,怎样在后台才能获取到mac地址全部的内容,包括:有谁知道linux系统环境下,怎样在后台才能获取到mac地址、如何查看linux系统mac地址、Windows或Linux下如何查看网卡MAC地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/web/9527615.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存