Hai’s Blog

April 30, 2009

One-Liner to Display My Computer’s IP Address

Filed under: Unix — Hai @ 6:02 am

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.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.