2012-04-03 18 views

答えて

9

まず、ルートデバイスが必要です。それが完了したらちょうどdnsmasq.leasesファイルを読んでください。通常は/data/misc/dhcp/dnsmasq.leasesに配置されます。 ファイルの構造はかなりシンプルです。各行は接続されたユーザーの要約です。要約には、MACを含むいくつかのフィールドがあります。 ルートなしでMACを取得する可能性はありませんでした。私が間違っているなら、私を修正してください。

+1

静的IPに接続されているクライアントはdhcpではありません。 dnsmasqには表示されません、あなたは何か考えていますか? – user935143

+0

このジョブを実行するための 'android'パッケージのクラスはありませんか? –

7

/proc/net/arpを読むと、最後の60秒間にデバイスと通信したスタティッククライアントとDHCPクライアントの両方の情報が提供されます(電話機のwl0.1/proc/sys/net/ipv4/neigh/wl0.1/gc_stale_timeに設定されています)。

root以外のユーザーも利用できます。

+2

これは/ proc/net/arpで真実ですか?デバイス(クライアント)がAP @Romanから切断されたとき、arpが常にリストを更新するとは限りませんか? – 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

あなたのコードが何をしているかについての追加情報を提供してください。何が起こっているのか誰にも分かりにくいです。 –

関連する問題