gpt4 book ai didi

java - 如何通过模式获取物理地址?

转载 作者:行者123 更新时间:2023-12-01 13:06:00 25 4
gpt4 key购买 nike

我想通过java中的模式识别来获取WiFi计算机的mac地址,但是这个putten给了我所有的mac地址(WIFI,以太网适配器......)可以帮助我只获取妻子的mac地址。

     public static  GetMac() {
byte[] macB = null;
byte[] bytesEncoded = null;
try {
String command = "ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader inn = new BufferedReader(new InputStreamReader(
p.getInputStream()));
//I want get just mac address
Pattern pattern = Pattern.compile(".*Physical Addres.*: (.*)");
while (true) {
String line = inn.readLine();

if (line == null)
break;
Matcher mm = pattern.matcher(line);
if (mm.matches()) {
String macS = mm.group(1);
macB = macS.getBytes();
bytesEncoded = Base64.encodeBase64(macB);
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

最佳答案

最简单的事情就是扫描 ipconfig 结果,其中包含 mac id 的行前面是 ->“物理地址............: “在 mac id 之前“00-00-00-00-00-00”。查找索引 ":" 在该行中添加 1,然后将该索引减去文件长度即可得到 Mac Output of ipconfig/all

批量

@echo
off
ipconfig /all
pause
exit

java代码

 package test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ProcessCommanLine {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ProcessCommanLine batchExecuteService = new ProcessCommanLine();
ProcessCommanLine.run();
}

static void run() {
// TODO Auto-generated method stub
try {
String cmds[] = {"D:\\test1.bat"};
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmds);
process.getOutputStream().close();
InputStream inputStream = process.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
BufferedReader bufferedrReader = new BufferedReader(inputstreamreader);
String strLine = "";
while ((strLine = bufferedrReader.readLine()) != null) {
// System.out.println(strLine);
String ph="Physical Address";
String subtring=null;
if(strLine.length()>ph.length())
subtring=strLine.substring(3,ph.length()+3);
if(strLine.contains(ph))
{
int i=strLine.indexOf(":")+1;
System.out.println(strLine.substring(i, strLine.length()));

}
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}

}

输出

Mac

关于java - 如何通过模式获取物理地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23268631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com