get the internet ip address - android

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?

Here's a Java snippet that might help:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();

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

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.

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]

Related

connect to ip:port usb-Ethernet adapter on android

I have an HTC One and i root it. I connect usb-Ethernet adapter with OTG cable to my phone and set ip address on it (eth0 192.168.9.1/24).
Then i connect it to my laptop with ethernet cable and set ip address from that subnet on my laptop(192.168.9.2/24). I can ping from both side(phone --> laptop and laptop --> phone)
then i write an android program to listen to port (33333):
private class SocketServerThread extends Thread {
static final int SocketServerPORT = 33333;
int count = 0;
#Override
public void run() {
try{
serverSocket = new ServerSocket(SocketServerPORT,50,deviceAddress);
socket = serverSocket.accept();
}
}
}
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "+ inetAddress.getHostAddress() + "\n";
try {
deviceAddress=inetAddress.getByName(inetAddress.getHostAddress());
// deviceAddrgroup=inetAddress.getAllByName(inetAddress.getHostAddress());
}catch (UnknownHostException e){
e.printStackTrace();
}
}
}
}
When i want to connect to that port with (telnet 192.168.9.1 33333) from my laptop to my phone, it is not possible and telnet wait until timeout.
I start troubleshooting :
1 - use telnet 192.168.9.1 33333 on Terminal Emulator on my phone and i can connect to that port.
2 - i connect my phone to laptop with Wifi and i can connect to port 33333 without problem.
3 - i want to capture packet on android eth0 with Tcpdump. I copy Tcpdump on system/bin and system/xbin. When i want to execute tcpdump on Terminal Emulator, i have permission dined error! (my phone is root, and i use "su" before run that command)
4 - i use netstat on Terminal Emulator it show protocol tcp6 (::ffff:192.168.9.1)
!!!!
my question :
1 - any body had this problem before that can not connect to port on Ethernet-usb adapter?
2 - because protocol on android is tcp6, is it possible that cause the problem?
3 - any body known what is the problem of tcpdump on phone with root permission?

Get the IP Address of the device

From inside my activity, I am trying to get the ip address of my device. I am using the following code to do this:
public static 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() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
But this function does not return me the correct ip address (possibly its returning the router ip and not the proxy ip given for my device). I went through lot of threads on StackOverflow but non of them helped.
Also, I dont plan to use :
WifiManager wifiMan = (WifiManager) this.getSystemService(this.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
int ipAddress = wifiInf.getIpAddress();
as in some cases my devices is wired connected and not wireless
Can someone suggest me how to get the correct ip address of my device from code?
Ideally, I would want that weather the device is wired/wireless connected, it should give the correct ip address of the device. In some situations, my device is wired connected.
Thanks for any help.

Can I get the IP address of the access point to I am connected to over wifi?

Hi I am new to android programming. I am basically trying to connect to an access point and send it come commands. After connecting to it over wifi, is it possible to programatically obtain it's IP address so that I can establish a http connection with it?
So far I know that we can obtain the device IP, but not sure if the access point IP can be obtained. Please help. Thanks in advance.
public static String getApIpAddr(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
byte[] ipAddress = convert2Bytes(dhcpInfo.serverAddress);
try {
String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress();
return apIpAddr;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
private static byte[] convert2Bytes(int hostAddress) {
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
return addressBytes;
}
I assume you mean the outside (public) IP address of the access point that the device is connected to. If so, yes there is a simple way to get the public IP address of the access point that a device is connected to. Simply setup a script on a web server that will echo back the IP address of any client that connects to it (similar to www.whatismyip.com). Then, your device just needs to do a GET request to the script, and this will return the outside IP of the access point that the device is connected to.
I am using this to get the IP address
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())
{
//My IP address
String Ip= inetAddress.getHostAddress().toString();
}
}
}
}
catch (SocketException e)
{
Log.e("Error occurred ", e.toString());
}

How do I get IP_ADDRESS in IPV4 format

I am trying to get the IP address of an device i.e using WIFI or 3G connection. I am getting the ip address in IPV6 format which is not understandable. I want in IPV4 format IP address.I have done google but dint found any proper solutions.
here is code which I am using to get IP address of an device
public String getLocalIpAddress() {
try {
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();
System.out.println("ip1--:" + inetAddress);
System.out.println("ip2--:" + inetAddress.getHostAddress());
if (!inetAddress.isLoopbackAddress()) {
String ip = inetAddress.getHostAddress().toString();
System.out.println("ip---::" + ip);
EditText tv = (EditText) findViewById(R.id.ipadd);
tv.setText(ip);
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception ex) {
Log.e("IP Address", ex.toString());
}
return null;
}
I am getting this ouput :
ip1--:/fe80::5054:ff:fe12:3456%eth0%2
ip2--:fe80::5054:ff:fe12:3456%eth0
It should be displayed like this :
192.168.1.1
please help me out..
After trying many tricks.. finally I can get the IP address in IPV4 format.. Here is my code..
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();
System.out.println("ip1--:" + inetAddress);
System.out.println("ip2--:" + inetAddress.getHostAddress());
// for getting IPV4 format
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {
String ip = inetAddress.getHostAddress().toString();
System.out.println("ip---::" + ip);
EditText tv = (EditText) findViewById(R.id.ipadd);
tv.setText(ip);
// return inetAddress.getHostAddress().toString();
return ip;
}
}
}
} catch (Exception ex) {
Log.e("IP Address", ex.toString());
}
return null;
}
Added if condition as shown below
/**This shows IPV4 format IP address*/
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())){}
instead of this
/**This shows IPV6 format IP address*/
if (!inetAddress.isLoopbackAddress()){}
Many Thanks..
Rahul
An alternative for checking if the address is a version 4 address is:
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address)
You cannot assume that any device has just one network address. You also cannot assume that it will have any IPv4 - it may be IPv6 only, so your application will need to be able to handle both IPv4 and IPv6 address displays.
Typically, an Android phone has at least two interfaces that get assigned usable ip addresses, rmnet0 for the 3G data, which for IPv4 is often carrier-grade NATed and so cannot accept incoming socket connections, and may also have an IPv6 address; and wlan0 for the wifi, which will have whatever IPv4 and/or IPv6 address it can negotiate with the network it attaches to.
Some versions of Android will intentionally drop the (typically more expensive) rmnet0 link when it attaches to wifi - in an attempt to reduce 3G data usage. This behaviour is a problem when the wifi has attached to something that is a captive portal that requires a manual sign-in.
It seems there is a seperate class Inet4Address in the Java API for IPv4 addresses.
Following is a simple way to check either if it's IPv4 or IPv6:
InetAddress address = InetAddress.getByName(ip);
if (address instanceof Inet6Address) {
// It's ipv6
} else if (address instanceof Inet4Address) {
// It's ipv4
}

android: get IP over WIFI on ICS

I want to get the IP used by WIFI connection(not 3G). Does anybody know how to do it?
I used:
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;
}
but it returns the 3G ip on ICS.
Thanks,
Alex
You cant detect connection type based on IP address because your mobile network and home WiFi network, both can assign private IP address.
What you need to do is to first detect either you have mobile network or WiFi connection, and then based on that info get the IP address of that connection.
See this thread in SO it's the same issue as your on ICS
WifiInfo winfo = ((WifiManager)this.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo();
winfo.getIpAddress();
You may want to check if you are connected to Wifi prior to doing the above

Categories

Resources