jump to navigation

Getting IP address from GNU/Linux command line November 19, 2010

Posted by Tournas Dimitrios in Linux.

Need to get your IP address from the command line with maximum geekiness? Two simple tools together can get the job done. This is handy for use as part of another script or just for satisfying curiosity.
$ curl -s http://checkip.dyndns.org | sed ‘s/[^0-9.]//g’
This is a simple one-two punch. First, we use the curl> tool to get back a HTML document that contains our IP, using the checkip service from DynDNS. Next, we need to filter the output to return just the IP address.

Then we pipe the output to sed>, with the instructions s/[^0-9.]//g. This tells sed to do the following:

  • invoke the (s)ubstitute command;
  • find the first character ([…]) which is a ([^…]) a digit (0-9) or a period (.);
  • replace each such character with an empty string, effectively removing it;
  • repeat this command (g)lobally for the entire stream

The result is our IP address.


No comments yet — be the first.

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