1

Is there a way to get the IP (v4) address of an interface in the file system of Android? I know I could use ifconfig or netcfg but I would like to use a monitoring tool which can only access files and not execute commands.

1

According to a post on SO, cat /proc/net/tcp will return it in

the second column, with the heading "local_address", e.g. "CF00A8C0:0203".

The part after ":" is a port number.

From the rest use the last two (C0) as a hex number, e.g. C0 is 192, which is the start of the address in this example.

Further:

The IP address is displayed as a little-endian four-byte hexadecimal number; that is, the least significant byte is listed first, so you'll need to reverse the order of the bytes to convert it to an IP address.

The port number is a simple two-byte hexadecimal number.

  • unfortunately that does not work on my phone. that file only has the headers but no data. i'm using android 4.4.2 on a SGS4. – AlvaHenrik Jan 30 '14 at 13:38
  • ah i see. there is only data if tcp connections are active. that's not so practical for my purpose. – AlvaHenrik Jan 30 '14 at 13:55
  • You could also check with /proc/net/arp, but I'm not sure if that has the very same restriction. What should work, however, is looking at the device information in /proc/net/dev, and picking the desired interface (e.g. tiwlan0 for WiFi in most cases). – Izzy Jan 30 '14 at 14:08
  • /proc/net/arp is empty as well on my phone and /proc/net/dev does only contain statistics but not the IP address. :-( – AlvaHenrik Jan 30 '14 at 15:04
  • Then I'm out of ideas. Until someone else can give a definite answer, you might do a ls on /proc/net (and /sys/...) to check for promising candidates. – Izzy Jan 30 '14 at 15:07
0

I have no Android to test it, but Linux offers:

 cat /proc/net/fib_trie

.. from https://unix.stackexchange.com/questions/365225/how-to-get-the-ipv4-address-for-an-interface-from-proc

The output can then be filtered further by grep, e.g.

 cat /proc/net/fib_trie|grep '|--' | grep -vE ' 127|\.0[^0-9.]*$|\.255[^0-9.]*$'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.