Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Ok, so I have a curl/awk one-liner that can get me my ip in shell scripts that uses checkip.dyndns.org. It's super simple because the results are super simple from checkip.

Anyone want to take a stab at this for google's result page?



This works, but only accidentally:

    curl --silent 'http://www.google.com/search?q=what+is+my+ip' | sed 's/.*Client IP address: //;s/).*//;q'
Google responds with HTTP error 403 (Forbidden), but it just so happens the client IP address is in the error page.


haha, nice one


Even simpler:

    curl icanhazip.com


also,

    http://myip.dnsomatic.com
    http://whatismyip.org



If your use-case is from ssh'ing in a remote box, this may be simpler:

  #!/bin/sh
  # example from `who - u`:
  #nick     pts/0  2011-07-20 20:44 . 7856 (xxxxxxx.xxxxxxxx.internode.on.net)

  who -u | awk 'BEGIN {FS="[p.]"} /internode/ {print gensub("-",".","g",$6); exit}'
You might need to play around with gensub(...). Sometime `who` reports the IP as xxx-xxx-xxx-xxx and sometimes as xxx.xxx.xxx.xxx


I prefer 'curl ifconfig.me'


One way:

    wget -Ufoo -qO- 'http://www.google.com/search?q=ip' |
    sed -n '/.*Your public IP address is [^0-9]*\([0-9.]*\).*/{s//\1/p;q;}'


Google really doesn't it like it when you script their search. Better to use one of the many, many services designed for this purpose. Or set one up yourself in roughly one line of code.


True, but the OP asked explicitly for it and no one else seemed to answer that question.





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: