-1

I need to get the internet ip address. For now i want to get it using the linux terminal, so i type "ifconfig". I'm connected by my android phone via thetering, and i've noticed there's not my internet ip address in the output of "ifconfig".

usb0      Link encap:Ethernet  HWaddr 6a:22:38:4d:92:36  
          indirizzo inet:192.168.42.79  Bcast:192.168.42.255  Maschera:255.255.255.0
          indirizzo inet6: fe80::6822:38ff:fe4d:9236/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:31359 errors:4 dropped:0 overruns:0 frame:4
          TX packets:27688 errors:0 dropped:0 overruns:0 carrier:0
          collisioni:0 txqueuelen:1000 
          Byte RX:30033107 (30.0 MB)  Byte TX:4855114 (4.8 MB)

This is the output of the command "ifconfig". Is there a universal way to get the ip address, by script commands or by "c" functions?

optimusfrenk
  • 1,271
  • 2
  • 16
  • 34

4 Answers4

1

Here's a Java snippet that might help:

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

Below is the code to get IP address via Java code (whether its connected via 3G or WIFI)

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}

Reference: Get the ip address of your device

If you want to get IP address in C, probably this thread would help:

using C code to get same info as ifconfig

Community
  • 1
  • 1
Chandra
  • 1,317
  • 11
  • 14
  • Hi Chandra, can i get the internet ip address , not for device ip. if mobile is connected to wifi or 3g or 2g or anyway. mobile having internet means, need that internet ip address please.. – harikrishnan Sep 20 '13 at 13:27
0

Your question is a bit vague. What sort of IP address are you looking for? If you're looking for your LAN IP, ifconfig or iwconfig will suffice. If you're looking for the WAN IP use

wget -qO- http://cmyip.com | grep "My IP Address Is"
and you should be able to see your IP. I don't know whether the Android terminal you're using supports wget but it's worth a shot.
hesson
  • 1,812
  • 4
  • 23
  • 35
0

The IP address after "inet:" i.e. 192.168.42.79 IS your IP address. But, that being said, the shortest python script is...

#!/usr/bin/python

import socket

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com',80))
return s.getsockname()[0]
James Rice
  • 75
  • 1
  • 10