Getting IP address from GNU/Linux command line

Summary How to get your IP address from the command line in one shot with curl and sed.

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'
209.145.68.249

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 not ([^...]) 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.

Trackbacks (none)

TrackBack URL: http://distilledb.com/mt/mt-tb.cgi/47

Comments (2)

nice article

i want be internet connected thr local lan in my laptop. how

leave new comment