LoginSignup
1
0

More than 5 years have passed since last update.

Sota(Edison版)ネットワーク内のipアドレスの取得方法

Last updated at Posted at 2017-08-11

通常javaだとInetAddressを利用するそうですが
例外が発生しうまく取得できなかったので以下のコマンドで取得する方法を試みました。

他にいい方法をご存知の方がいらっしゃいましたら、ご教示お願い致します。

    public static String getLocalIpAddr() {
        return SotaUtils.getLocalIpAddr("wlan0");
    }

    public static String getLocalIpAddr(String interfaceName) {
      try {
        Runtime runtime = Runtime.getRuntime();
        Process p = runtime.exec("ip route");
        InputStream is = p.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String b = null;
        int i = 0;
        while ( ( b = reader.readLine() ) != null ) {
            if( i == 0) { i++; continue; }
            if( b.contains(interfaceName) ) {
                String[] split = b.split( " src ", 0);
                if( split.length >= 1 ) {
                    return split[ split.length - 1 ];
                }
            }
            i++;
        }
      } catch (IOException ex) {
      }
        return null;
}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0