I was randomly reading when I saw this with my head shaking:

curl -s "https://www.google.com/search?q=what+is+my+ip+address" |
grep "Client IP address:" |
sed 's/.*Client IP address: //g;s/).*//g')

Note

I decided not to link to the blog post, but I’ve saved the URL with this blog post source. If you really need it, you may ask.

First of all, Google seems to have blocked both curl and wget—or just any request with unacceptable user agent—returning 403, if you do not change user agent. So, it’s actually grepping on an error page which happened to be providing the client IP address. You don’t need a full question, just “foobar” as query string can trigger Google giving you your IP address since the client program isn’t acceptable. Normally, if you are using web browser to search, “ip” would do.

Secondly, the grep and sed are too long, the following does the same:

curl -s "https://www.google.com/search?q=a" |
sed -n 's/.*Client IP address: \([^)]\+\).*/\1/p'

I don’t like this way at all, extracting from an HTML of 403 page, 5.1KB for just 15 characters at most. It’s such a waste and not to mention your computer need to sed the HTML file.

There are already plenty of one-liners and services around, so I decided to write get-my-ip.sh or to collect those, so to speak:

It can runs with dry run option, just to check:

$ ./get-my-ip.sh -nv
admin_linux_fr  : <IP>
curlmyip        : <IP>
externalip      : <IP>
icanhazip       : <IP>
ifconfig_me_ip  : <IP>
ip_api          : <IP>
ip_appspot      : <IP>
ipinfo_io       : <IP>
myip_opendns    : <IP>
telize          : <IP>
trackip         : <IP>

Verbosely for real run, by default, it queries through all services:

$ ./get-my-ip.sh -v
admin_linux_fr  : <removed>.<removed>.59.174
curlmyip        : <removed>.<removed>.59.174
externalip      : <removed>.<removed>.59.174
icanhazip       : <removed>.<removed>.59.174
ifconfig_me_ip  : <removed>.<removed>.59.174
ip_api          : <removed>.<removed>.59.174
ip_appspot      : <removed>.<removed>.59.174
ipinfo_io       : <removed>.<removed>.59.174
myip_opendns    : <removed>.<removed>.59.174
telize          : <removed>.<removed>.59.174
trackip         : <removed>.<removed>.59.174

Or pick services you want:

$ ./get-my-ip.sh ip_appspot telize
<removed>.<removed>.59.174
<removed>.<removed>.59.174

I don’t think anyone would need to have full script, so I placed the code in public domain, you can just take the line you need. Feel free to add more if you have some.