在android4.3中如何利用程式碼抓取ethernet dns ip呢?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

在android4.3中如何利用程式碼抓取ethernet dns ip呢?

Jump to solution
4,776 Views
陳冠儒
Contributor IV

hi,All:

請問在android4.3中如何利用程式碼抓取dns ip呢?

因為LinkProperties的getDnsServers()必須在android5.0上用,

DhcpInfo class的所有函式似乎也是不能用於ethernet

就目前認知是如此

所以不知各位大大  有任何的方式可以解決ethernet 取得dns ip的問題嗎?

感謝大家~~~

Labels (4)
1 Solution
4,282 Views
JayTu
NXP Employee
NXP Employee

android 已經把 ip 存在 property裡,你可以在 console 下試試,

getprop net.dns1 (or net.dns2)

寫 code 的話,你可以從 android.os.SystemProperties 取得

View solution in original post

10 Replies
4,283 Views
jimmychan
NXP TechSupport
NXP TechSupport
0 Kudos
4,283 Views
陳冠儒
Contributor IV

hi,jimmychan:

感謝您的回答,不過在寫code的情況下有沒有可以抓到DHCP  dns ip的方法或是函式可用呢?

您提供的網站我看了一下,是使用getByName()函式  利用domain name 取得IP ,我也想過這個方式所以我利用

ConnectivityManager mConnectivityManager  = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

LinkProperties networkDns = mConnectivityManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET);

String Domainname = networkDns.getDomains();

InetAddress DnsAddress = InetAddress.getByName(Domainname);

String DnsAddr = DnsAddress.toString();

然後將DnsAddr 秀出到螢幕上

但是失敗轉不出ip address,查到Domainname會是空值,表示networkDns.getDomains(); 沒抓到正確的Domain name,這裡是否因為LinkProperties的getDnsServers()必須在android5.0上用,也導致出問題呢?

不知您還有其他方式可以解決抓取DHCP Dns ip的問題嗎?

再麻煩您幫忙一下了,感謝您。

0 Kudos
4,283 Views
JayTu
NXP Employee
NXP Employee

android 已經把 ip 存在 property裡,你可以在 console 下試試,

getprop net.dns1 (or net.dns2)

寫 code 的話,你可以從 android.os.SystemProperties 取得

4,283 Views
陳冠儒
Contributor IV

hi,JayTu:

想再請教一下,寫code的話我是已經可以取得dns ip如下:

SystemProperties.get("net.dns1");

也用

Strung Dnsaddr = "1.1.1.1";

SystemProperties.set("net.dns1", Dnsaddr);

設定新的ip

但我有辦法連ip、subnetmask、gateway都寫回系統嗎?

因為我目前查似乎

ip不是net.ip可以存的

subnetmask也不是net.subnetmask可以存的

gateway也不是net.gateway可以存的

所以導致我用

SystemProperties.set("net.ip", Dnsaddr); 也只是寫入net.ip參數內  並未真正改變ip的位址

因為我利用ifconfig eth0 看到ip還是沒改成1.1.1.1

故這要怎麼做才能寫回系統,讓我下次打ifconfig eth0 讓我看到相關的改變呢?

原因是我想寫LAN靜態IP的設定介面這樣,再麻煩您幫幫忙了,感謝您。

感恩!!!

0 Kudos
4,283 Views
JayTu
NXP Employee
NXP Employee

What you wanna do is included on our sample apk, ethernet.apk. You can refer to it for more detail.

0 Kudos
4,283 Views
陳冠儒
Contributor IV

hi,JayTu:

想再請教一下,我是在demo code(sample code)中(packagees/apps/fsl_imx_demo/Ethernet/com/fsl/ethernet/下)一支EthernetManager.java中提到兩段如下:

public synchronized void updateDevInfo(EthernetDevInfo info) {
        SystemProperties.set("net.dns1", info.getDnsAddr());
        SystemProperties.set("net." + info.getIfName() + ".dns1",info.getDnsAddr());
        SystemProperties.set("net." + info.getIfName() + ".dns2", "0.0.0.0");
        SystemProperties.set("net." + info.getIfName() + ".config", "1");
        SystemProperties.set("net." + info.getIfName() + ".mode", info.getConnectMode());
        SystemProperties.set("net." + info.getIfName() + ".ip", info.getIpAddress());
    }

public synchronized EthernetDevInfo getSavedConfig() {
        if (!isConfigured())
            return null;

        EthernetDevInfo info = new EthernetDevInfo();
        info.setConnectMode(SystemProperties.get("net."+ DevName[0] + ".mode", ""));
        info.setIpAddress(SystemProperties.get("net."+ DevName[0] + ".ip", ""));
        info.setDnsAddr(SystemProperties.get("net."+ DevName[0] + ".dns1", ""));
        //info.setNetMask(Settings.Secure.getString(cr, Settings.Secure.ETHERNET_MASK));
        //info.setRouteAddr(Settings.Secure.getString(cr, Settings.Secure.ETHERNET_ROUTE));
        info.setIfName(DevName[0]);
        return info;
    }

但我看好像沒有直接將ip、subnetmask、gateway都寫回系統,故這3項要怎麼寫回系統,不然打ifconfig eth0 都沒變化,可否寫得明白一點好讓我了解怎麼做,抱歉這部份初做不熟,請您見諒。

主要是我想寫LAN靜態IP的設定介面這樣,再麻煩您幫幫忙了,感謝您。

感恩!!!

0 Kudos
4,283 Views
JayTu
NXP Employee
NXP Employee

Can you confirm if the demo apk work on your board or not? It's test on our reference board, if it's not working on your board, it's another issue. 

If it's working, you can trace it since it's not complex. 

0 Kudos
4,283 Views
陳冠儒
Contributor IV

hi,JayTu:

我目前是已成功可寫入ip跟dns到系統內,所以下getprop net.dns1跟ifconfig eth0可以看到dns跟ip有變化,但netmaskgateway尚無法有效寫入系統,請教您該如何做呢?

因為我利用兩個指令:

ifconfig eth0

ip route show

皆看不到netmaskgateway有所改變,故不知您有方式可以解決這個問題嗎?

再麻煩您幫幫忙了,感謝您!!!

感恩!!!

0 Kudos
4,283 Views
陳冠儒
Contributor IV

hi,JayTu:

我試過可以執行得起來產生出一個黃底的ethernet APK圖示,執行它後卻發現無法正確地取得ip,故此demo(sample) code似乎是不太能在imx6q的板子上動作,有點奇怪,是否跟我在build code時用 lunch sabresd_6dq-user有關,是否此sample code不能在user下執行呢?

再請教您sample code中ip是用

 SystemProperties.set("net." + info.getIfName() + ".ip", info.getIpAddress());  回存

跟下面紅字那行在設定IP

void configureInterface(EthernetDevInfo info) {
      .....

        } else {
            InterfaceConfiguration ifcg = null;
            Log.d(TAG, "Static IP =" + info.getIpAddress());
            try {
                ifcg = mNMService.getInterfaceConfig(info.getIfName());
                ifcg.setLinkAddress(new LinkAddress(NetworkUtils.numericToInetAddress(info.getIpAddress()), 24));
                ifcg.setInterfaceUp();
                mNMService.setInterfaceConfig(info.getIfName(), ifcg);

                Log.d(TAG,"Static IP configuration succeeded");
            } catch (RemoteException re){
                Log.e(TAG,"Static IP configuration failed: " + re);
            } catch (IllegalStateException e) {
                Log.e(TAG,"Static IP configuration fialed: " + e);
            }
           ....
   

那我應該用那種方式才能真正改變系統的IP  讓ifconfig eth0 這個指令能查到新設定的靜態IP(因為SystemProperties似乎沒辦法真正改變IP設定)?

而我看過這幾支sample code後並未發現有回存 gateway跟 subnetmask故這兩項又要如何回存系統內呢?

不然利用

ifconfig eth0 -->eth0的ip跟subnetmask

ip route show -->eth0的gateway ip

抓取IPsubnetmaskgateway時會發現靜態IP設定的值沒被改變回系統,所以不知該如何才能做到呢?

再麻煩您幫幫忙了,感謝您。

感恩!!!

0 Kudos
4,283 Views
陳冠儒
Contributor IV

hi,JayTu:

感謝您的回答,經試過後的確可以抓到DNS IP ,謝謝您的幫忙,感恩。

0 Kudos