当我的手机处于Wi-Fi网络共享模式时,有没有办法获取已连接的MAC地址列表?
答案 0 :(得分:9)
首先,您必须拥有root设备。完成后只需阅读 dnsmasq.leases 文件即可。通常它位于: /data/misc/dhcp/dnsmasq.leases 。 该文件的结构非常简单 - 每一行都是连接用户的摘要。摘要有几个字段,包括MAC。 我没有找到没有root的MAC的可能性。如果我错了,请纠正我。
答案 1 :(得分:7)
阅读/proc/net/arp
将提供在过去60秒内与设备通信的静态和DHCP客户端的信息(在/proc/sys/net/ipv4/neigh/wl0.1/gc_stale_time
中配置,其中wl0.1
是无线网络接口我的手机)。
它也适用于非root用户。
答案 2 :(得分:1)
@SuppressWarnings("ConstantConditions")
public static String getClientMacByIP(String ip)
{
String res = "";
if (ip == null)
return res;
String flushCmd = "sh ip -s -s neigh flush all";
Runtime runtime = Runtime.getRuntime();
try
{
runtime.exec(flushCmd,null,new File("/proc/net"));
}
BufferedReader br;
try
{
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null)
{
String[] sp = line.split(" +");
if (sp.length >= 4 && ip.equals(sp[0]))
{Assistance.Log(sp[0]+sp[2]+sp[3],ALERT_STATES.ALERT_STATE_LOG);
String mac = sp[3];
if (mac.matches("..:..:..:..:..:..") && sp[2].equals("0x2"))
{
res = mac;
break;
}
}
}
br.close();
}
catch (Exception e)
{}
return res;
}
// --------------------------------------------- -----------
@SuppressWarnings("ConstantConditions")
public static String getClientIPByMac(String mac)
{
String res = "";
if (mac == null)
return res;
String flushCmd = "sh ip -s -s neigh flush all";
Runtime runtime = Runtime.getRuntime();
try
{
runtime.exec(flushCmd,null,new File("/proc/net"));
}
BufferedReader br;
try
{
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null)
{
String[] sp = line.split(" +");
if (sp.length >= 4 && mac.equals(sp[3]))
{
String ip = sp[0];
if (ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}") && sp[2].equals("0x2"))
{
res = ip;
break;
}
}
}
br.close();
}
catch (Exception e)
{}
return res;
}
答案 3 :(得分:0)
public static ArrayList<String> getConnectedDevicesMac()
{
ArrayList<String> res = new ArrayList<String>();
//NetManager.updateArpFile();
BufferedReader br;
try
{
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
line = br.readLine();
while ((line = br.readLine()) != null)
{
String[] sp = line.split(" +");
if (sp[3].matches("..:..:..:..:..:.."))
res.add(sp[3]);
}
br.close();
}
catch (Exception e)
{}
return res;
}