Getting IP, Subnet Mask, DNS and Default Gateway

Status
Not open for further replies.

DKCERT

Member
Licensed User
Longtime User
Hey all,

I would like to get all the basic network settings for a given device.

The IP addy is easy: ServerSocket.GetMyIp. But how do I get the Subnet mask, DNS-server(s) and the Default Gateway?

The normal Default Gateway can be calculated by using the IP and Subnet mask - but in some scenarios it can be placed strangely in the ip-scope. That's why I would like to get the assigned one.

So is there a way to get this information? Maybe using the reflection lib? or?

Thanks in advance.
 
Last edited:

rtek1000

Active Member
Licensed User
Longtime User
Have any other way to get the subnet mask?

I received errors with this phone:

Model: XT1069
Android version: 6.0
SDK: 23
Wifi Interface: wlan0
IP Address:
Subnet Mask:
Broadcast Addr.: -1.
Default Gateway:
DNS #1:
DNS #2:
Lease time: minutes

Point12
(ArrayIndexOutOfBoundsException) java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
Point13
(NumberFormatException) java.lang.NumberFormatException: Invalid double: ""

B4X:
        Log("Point12")
       
        Try
            For i = 0 To 3
                iIP = Val(sCurrentIP(i).Trim)
                iNetMask = Val(sNetMask(i).Trim)
                sTmp = DecToBin(iNetMask,8)
                sTmp = ReverseBits(sTmp)
                iNetMask = BinToDec(sTmp)
                sBCAddr = sBCAddr & Bit.Or(iIP,iNetMask)
                If i < 3 Then sBCAddr = sBCAddr & "."
            Next
        Catch
            Log(LastException)
        End Try

       
        ProgressDialogHide
       
        Log("Point13")

        Try
            sLease = NumberFormat(sLease / 60, 0, 2)
        Catch
            Log(LastException)
        End Try
       
                               
        Log("Point14")
       
        Return  "Model: " & sModel & CRLF & _
                                        "Android version: " & sVersion & CRLF & _
                "SDK:    " & sSdk & CRLF & CRLF & _
                "Wifi Interface: " & sWifi & CRLF & _
                "IP Address: " & sIP & CRLF & _
                "Subnet Mask: " & sMask & CRLF & _
                "Broadcast Addr.: " & sBCAddr & CRLF & _
                "Default Gateway: " & sGate & CRLF & _
                "DNS #1: " & sD1 & CRLF & _
                "DNS #2: " & sD2 & CRLF & _
                "Lease time: " & sLease & " minutes" & CRLF
    Else
        Return "Wifi Interface not found."
    End If
 
Upvote 0

rtek1000

Active Member
Licensed User
Longtime User
Well, I installed a shell terminal app, and ran the "getprop" command, it really is not listed to netmask in the data it returned.

But I can see netmask with the "ifconfig" command
 
Upvote 0

rtek1000

Active Member
Licensed User
Longtime User
Works for me:

B4X:
Dim ServerSocket1 As ServerSocket
Dim sNetMask1 As String

B4X:
    sb.Initialize
    P.Shell("ifconfig", Null, sb, Null)
    ' Log(sb.ToString)
 
    '"inet addr:192.168.1.30  Bcast:192.168.1.255  Mask:255.255.255.0"
    PropData = Regex.Split(CRLF,sb.ToString)
 
    For i = 0 To PropData.Length - 1
        '
        If PropData(i).Contains("inet addr:" & ServerSocket1.GetMyIP) Then
            sNetMaskPoint1 = PropData(i).IndexOf("Mask:")
            sNetMask1 = PropData(i).SubString(sNetMaskPoint1 + 5)
            Log(sNetMask1)
            Exit
        End If
    Next
 
Upvote 0

udg

Expert
Licensed User
Longtime User
While at regex you could even try the Matcher. Something like:
B4X:
..
Dim pattern As String
pattern = "Mask:\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"
Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, New)
Log(Matcher1.Match) '-->Mask:255.255.255.0
..

Note: above pattern would accept irregular values (999.888.777.987), but since you precede it by shell ifconfig command that shouldn't be a problem.
 
Upvote 0
Status
Not open for further replies.
Top