2012-04-03 2 views

답변

9

먼저 루트 장치가 있어야합니다. 완료되면 dnsmasq.leases 파일을 읽으십시오. 보통은 /data/misc/dhcp/dnsmasq.leases에 있습니다. 파일의 구조는 매우 단순합니다. 각 줄은 연결된 사용자의 요약입니다. 요약에는 MAC을 포함한 여러 필드가 있습니다. 루트없이 MAC을 얻을 수있는 가능성을 찾지 못했습니다. 내가 틀렸다면 나를 바로 잡아주세요.

+1

정적 IP에 연결된 클라이언트는 dhcp가 아니며 어떡합니까? dnsmasq에 나타나지 않을거야, 너는 어떤 생각이있어? – user935143

+0

이 작업을 수행 할 'android' 패키지의 클래스가 없습니까? –

7

읽기 /proc/net/arp은 지난 60 초 동안 기기와 통신 한 정적 및 DHCP 클라이언트에 대한 정보를 제공합니다 (/proc/sys/net/ipv4/neigh/wl0.1/gc_stale_time으로 설정, wl0.1은 휴대 전화의 무선 네트워크 인터페이스 임).

루트가 아닌 사용자도 사용할 수 있습니다.

+2

/proc/net/arp가 장치 (클라이언트)가 AP @Roman과의 연결을 끊었을 때 항상 목록을 업데이트하는 것은 아닙니다. – gumuruh

0
@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; 
} 
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; 
} 
+0

코드에서 수행중인 작업에 대한 추가 정보를 제공해주십시오. 새로운 일이 일어나고 있는지 알리는 것은 어렵습니다. –