How to get all IP addresses in python?

Roy Smith roy at panix.com
Fri Jul 11 07:46:13 EDT 2003


"Yun Mao" <maoy_REMOVE_IF_NOT_SPAM at cis.upenn.edu> wrote:
> Hi. I did a little homework on google for this question. The answer was:
> >>> import socket
> >>> hostname = socket.gethostname()
> >>> ip = socket.gethostbyname(hostname)
> >>> print ip
> 192.168.55.101
> But what if I have several interfaces, say eth0, eth0:1, eth1, etc. How to
> get all of them? It would be a little undesirable if people suggest to parse
> the result of ifconfig because I in particular want the code to be cross
> platform. Thanks a lot!

You might try using gethostname() as above to get the canonical host 
name for your machine, then using one of the various Python DNS modules 
to look up all the A records for that name.  Not perfect, but a start.




More information about the Python-list mailing list