1

I don't want to use the ifconfig because the output is not same in all distros: I want a consistent way to find the IP address of all the interfaces.

I was planning to get the interface name by parsing the /proc/net/dev and then using interface name to find the IP address using the siocgifconf ioctl found here . But that too is said, not to work on all version of Linux.

I'm looking something that doesn't use any non-default Python libraries like netifaces. And the ioctl is said not to work in all the versions of Linux.

Nick T
  • 25,754
  • 12
  • 83
  • 121
3lokh
  • 891
  • 4
  • 17
  • 39
  • 1
    Possible duplicate of http://stackoverflow.com/questions/6243276/how-to-get-the-physical-interface-ip-address-from-an-interface (which also has a link to another possible dupe!) – AlG Oct 06 '15 at 12:15
  • @AIG I'm looking something that don't use any non-standard libraries. And the ioctl is said not to work in all the versions of Linux, so it's not a duplicate question. – 3lokh Oct 06 '15 at 12:19
  • I don't see how you're going to get much better than `netifaces`, it seems like it was created explicitly to do what you want: be as platform independent as possible. If your favorite distro doesn't work with it, how about filing an issue/pull request? – Nick T Oct 06 '15 at 17:30
  • 1
    @AlG given his stdlib requirement, you pointed to the wrong dupe. At least use the other one: https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib – Nick T Oct 06 '15 at 17:35

2 Answers2

0

You can use the netifaces package which can be installed using easy_install:

from netifaces import interfaces, ifaddresses, AF_INET
for ifaceName in interfaces():
addresses = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )]
print '%s: %s' % (ifaceName, ', '.join(addresses))
Alex
  • 21,273
  • 10
  • 61
  • 73
  • It is not easy to get the address(es) of the machine’s network interfaces in a platform independent way. If you can't use `netifaces`, you may want to check out its source code: https://bitbucket.org/al45tair/netifaces – Alex Oct 06 '15 at 12:27
0

If your question was "How to get all IP addresses of my computer", I would answer

>>> import socket
>>> socket.gethostbyname_ex(socket.gethostname())
('strauch', [], ['172.16.0.100', '192.168.157.1', '192.168.60.1'])

It can not tell the interfaces but uses all of them.

User
  • 14,131
  • 2
  • 40
  • 59
  • 1
    But it's showing only loopback IP, it's not showing my interface IP which is 192.168.1.3. – 3lokh Oct 06 '15 at 12:36