Getting IP address from GNU/Linux command line November 19, 2010Posted 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.