One-Liner to Display My Computer’s IP Address

Here is yet another way to diplay a list of IP address associated with the current computer.

python -c "import socket; print '\n'.join(socket.gethostbyname_ex(socket.gethostname())[2])"

The socket.gethostname() call returns the host name of the computer. The socket.gethostbyname_ex() call returns a list of three items: The host name, the list of aliases for this host, and a list of IP addresses. Recall that Python’s array starts with index 0, the socket.gethostbyname_ex(…)[2] expression refers to the list of IP addresses. Finally, the print statement prints out the IP addresses, one per line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s