Get public IP from bash

In case anyone finds this useful, sometimes I need to get my public IP from bash or scripts and I use api.myip.com, only because it's easy to remember.

But more and more often lately, some IPs are blocked there and get http errors, so I bought a cheap domain and I woke up a $3/year @natvps_uk from idling, and..

wget -4 -qO- myip.cam
or for ipv6
wget -6 -qO- myip.cam

(or curl -4L myip.cam if curl is your thing)

Comments

  • I've always used ifconfig.me, it's quite easy to remember. I use da.gd/ip for IPv6 though.

  • @Wonder_Woman said:
    I've always used ifconfig.me, it's quite easy to remember. I use da.gd/ip for IPv6 though.

    Yeah I wouldn't remember da.gd/ip even if my life was depending on it :)

  • @itsdeadjim said:

    @Wonder_Woman said:
    I've always used ifconfig.me, it's quite easy to remember. I use da.gd/ip for IPv6 though.

    Yeah I wouldn't remember da.gd/ip even if my life was depending on it :)

    That's very true, I often bungle the letters up myself...

  • edited March 2023

    @itsdeadjim said:
    In case anyone finds this useful, sometimes I need to get my public IP from bash or scripts and I use api.myip.com, only because it's easy to remember.

    But more and more often lately, some IPs are blocked there and get http errors, so I bought a cheap domain and I woke up a $3/year @natvps_uk from idling, and..

    wget -4 -qO- myip.cam
    or for ipv6
    wget -6 -qO- myip.cam

    (or curl -4L myip.cam if curl is your thing)

    I need to check IPs of VPN docker container to see if IP really changed or not.
    I use
    curl https://ifconfig.co/ip for ipv4
    curl -6 https://ifconfig.co/ip for ipv6
    But I could not remember it. I always copy paste from a txt file.

    Your's is quite easy to remember.
    This will be quite handy.
    Thanks for it <3

    BTW
    curl -4L myip.cam is for IPv4 I guess?
    What's curl command for IPv6 ?

  • ip.sb is also a good choice.

  • @adwsislife said:
    BTW
    curl -4L myip.cam is for IPv4 I guess?
    What's curl command for IPv6 ?

    curl -6L myip.cam

    Btw, the -L is needed because nat http proxy redirects http to https.
    If I find a vps with a public IPv4 for under $10/year, I will have both 443 and 80 reply, so -L will not be needed

    Good idea, I also enabled

    curl -L 4.myip.cam
    curl -L 6.myip.cam
    
  • curl -s https://checkip.amazonaws.com

    "How miserable life is in the abuses of power..."
    F. Battiato ---

  • @itsdeadjim said:
    But more and more often lately, some IPs are blocked there and get http errors, so I bought a cheap domain and I woke up a $3/year @natvps_uk from idling, and..

    It's worth reading about previous people who've run these kinds of service. Once they got popular, they start consuming a lot of bandwidth, so it might not be best to advertise it too widely... e.g. the one I just posted: https://blog.apnic.net/2021/06/17/how-a-small-free-ip-tool-survived/

  • edited March 2023

    @ralf said:

    @itsdeadjim said:
    But more and more often lately, some IPs are blocked there and get http errors, so I bought a cheap domain and I woke up a $3/year @natvps_uk from idling, and..

    It's worth reading about previous people who've run these kinds of service. Once they got popular, they start consuming a lot of bandwidth, so it might not be best to advertise it too widely... e.g. the one I just posted: https://blog.apnic.net/2021/06/17/how-a-small-free-ip-tool-survived/

    Lol crazy, I wouldn't expect that. Thanks!

    And this is why we can't have nice things.

  • FritzFritz Behlnd you

    What a weird name, never use it before.

  • curl -4 icanhazip.com
    curl -6 icanhazip.com
    

    These are owned by Cloudflare and operated on Workers platform
    https://major.io/2021/06/06/a-new-future-for-icanhazip/

    We accept Karma donations for the last flan. 🍮 affbrr

  • @Fritz said:

    What a weird name, never use it before.

    https://en.wikipedia.org/wiki/I_Can_Has_Cheezburger?

    It was the dominant meme on the internet for about 6 months about 15 years ago.

  • ipinfo.io

  • If you're bored, then try this:

    https://myip.wtf/

     curl myip.wtf/json
    
  • bash?
    Real G's use zsh

  • You can set up your own service via cf workers too

    Only gotcha is it returns whatever resolves faster...so sometimes ipv4 sometimes 6

  • @yoursunny said:

    curl -4 icanhazip.com
    curl -6 icanhazip.com
    

    These are owned by Cloudflare and operated on Workers platform
    https://major.io/2021/06/06/a-new-future-for-icanhazip/

    An interesting, educational, and entertaining story. Thanks for sharing.

  • @yoursunny said:
    These are owned by Cloudflare and operated on Workers platform
    https://major.io/2021/06/06/a-new-future-for-icanhazip/

    This is so cool!

  • @itsdeadjim said:
    If I find a vps with a public IPv4 for under $10/year, I will have both 443 and 80 reply, so -L will not be needed

    https://cloudserver.net/billing/index.php?rp=/store/custom-packages/leb-1gb-annual-plan

  • curl ipinfo.io
    curl v6.ipinfo (v6)

  • @bruh21 said:

    @itsdeadjim said:
    If I find a vps with a public IPv4 for under $10/year, I will have both 443 and 80 reply, so -L will not be needed

    https://cloudserver.net/billing/index.php?rp=/store/custom-packages/leb-1gb-annual-plan

    Thanks, It turned out I had another one with a public IP that was auto-renewing idling so I just moved the service there.

    Finally, the following requests are possible

    curl myip.cam
    curl 4.myip.cam
    curl 6.myip.cam
    curl -4 myip.cam
    curl -6 myip.cam
    

    By the way the service is a quick 'n' dirty custom C# async web-server behind an nginx reverse proxy. It seems that it can do 1-1.5k requests per second without much of trouble. (famous last words)

  • @shallow said:
    bash?
    Real G's use zsh

    Yeah good point. I use bash so long that it's become a synonym for terminal

  • atomiatomi OG
    edited March 2023

    @itsdeadjim said:

    By the way the service is a quick 'n' dirty custom C# async web-server behind an nginx reverse proxy. It seems that it can do 1-1.5k requests per second without much of trouble. (famous last words)

    You could do it with pure nginx
    location / { default_type text/plain; return 200 "$remote_addr"; }

  • edited March 2023

    @atomi said: You could do it with pure nginx
    location / { default_type text/plain; return 200 "$remote_addr"; }

    I remember a few years back I wanted to do this exact thing, a simple text output ip lookup. It seemed so simple, but wasn't (for me). I wish I knew about this little nginx trick then.

  • You can do it in Caddy with something that looks like:

    yourdomain.com {
        templates
        header Content-Type text/plain
        respond "{{.RemoteIP}}"
    }
    
  • altalt
    edited March 2023

    @tjn said:
    You can do it in Caddy with something that looks like:

    yourdomain.com {
        templates
        header Content-Type text/plain
        respond "{{.RemoteIP}}"
    }
    

    Oh, I did that same thing :p
    https://geoip.mxtrans.net/ip for plain ip return,
    https://geoip.mxtrans.net/json for geoip json format.
    Also short for ip.mx.sb, but you need add -L for redirection.
    curl -L ip.mx.sb

  • @Wonder_Woman said:
    I've always used ifconfig.me, it's quite easy to remember. I use da.gd/ip for IPv6 though.

    Yeah, I've been using ifconfig.me for years. It's neat and well enough for me.

  • @atomi said:
    You could do it with pure nginx
    location / { default_type text/plain; return 200 "$remote_addr"; }

    I have used a very simple php script for years to accomplish just this, I cant believe I did not think of doing it this way instead.
    It's just so simple it's brilliant, thank you.

  • @atomi said:

    is there "the simple" way to show all the payload... i mean if i did a get/post request , i am interested in seeing in json format the payload "echo back in json"

  • atomiatomi OG
    edited March 2023

    @ehab said:

    @atomi said:

    is there "the simple" way to show all the payload... i mean if i did a get/post request , i am interested in seeing in json format the payload "echo back in json"

    well there is echo module for nginx which definitely help you with more complex json. If all you want is IP in json format you could try something like this with pure nginx(untested)

    location /json {
      default_type application/json;
      return 200 '{"ipaddress":"'$remote_addr'"}';
    }
    
  • It's also possible via DNS:

    For IPv4:

    dig -4 +short -t a myip.opendns.com @resolver1.opendns.com
    

    And for IPv6:

    dig -6 +short -t aaaa myip.opendns.com @resolver1.opendns.com
    
  • host -4 myip.opendns.com resolver1.opendns.com
    host -6 myip.opendns.com resolver1.opendns.com

    Happy customer at AlexHost, AxusHost, Bakker-IT, Host-C, Ionos, Veesp + NanoKVM ftw.

  • curl ip.sb
    
  • curl ipinfo.io is the most useful

  • dfroedfroe Provider

    You can also avoid all the HTTP 'overhead' by using a simple DNS query to achieve the same:

    alias myip4="dig @208.67.220.220  @208.67.222.222  myip.opendns.com A    +short"
    alias myip6="dig @2620:119:35::35 @2620:119:53::53 myip.opendns.com AAAA +short"
    

    it-df.net: IT-Service David Froehlich | Individual network and hosting solutions | AS39083 | RIPE LIR services (IPv4, IPv6, ASN)

Sign In or Register to comment.