I want to know my Public IP address of PI using script and script should be start running automatically when my raspberry pi boot and Store output in some file, so that i can use later on.

Actually, I am Controlling my PI Remotely from Cloud and for that whenever Public IP of PI change then PI should be able to inform new IP to cloud. For that i need script that start automatically after boot and run every 5-10 second and check Public IP and overwrite output file.

I use sudo curl icanhazip.com for check public IP but that give me

Output of my script with public IP at the end

actually i need only public IP without extra information, is there any command that give me only public IP and for looping a script i use While loop. Is it a right way or is there another better way to run screen in a loop.

  • From what you described, it seems that DDNS is the service that you need. It a simple step of setting it up than trying to do this via programming. You should check with your cloud service provider on DDNS, or even set it up on your router.
    – hcheung
    Commented Apr 28, 2018 at 1:35
  • If you cloud service provider can provide DDNS, all you need on the RPi is to install ddclient. I have a blog which has a section on how to setup DDNS.
    – hcheung
    Commented Apr 28, 2018 at 1:41

2 Answers 2


Use the documented -s or --silent option

sudo curl -s icanhazip.com


sudo curl --silent icanhazip.com
  • Thanks a lot. that's true. Working Perfectly. Do you have any idea how can i run script on boot ?
    – prince
    Commented Apr 27, 2018 at 8:24
  • Oh. I didn't realise you had an issue with that as well. /etc/rc.local is one way Commented Apr 27, 2018 at 8:45
  • Great ! That's work perfectly as well. Thanks a lot.. !!
    – prince
    Commented Apr 27, 2018 at 9:29
  • is it a right way to loop script every 5 second using while loop or do you have any other suggestion ?
    – prince
    Commented Apr 27, 2018 at 9:30

It seems your question for getting your ip address has been solved, but that there's still a question re "looping" this command. Here's a suggestion for that bit:

Add the command to your crontab, and let cron run it for you at any interval you specify. Here are the steps from your RPi terminal:

crontab -e

This will display your current (likely default in your case) crontab file. Note that each line is "commented out" with # in column 1.

In pico (or whatever your default editor is), add a line to tell cron your schedule. In your case, adding the following crontab entry will run your command every minute.

* * * * *  sudo curl -s icanhazip.com

And no, you can't do it every 5 seconds! You'll need another solution if that's an actual requirement, but I can't even imagine why you'd even need updates every minute since DHCP leases usually last far longer.

And finally, if you want to run this on reboot, put this line in your crontab:

@reboot curl -s icanhazip.com 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.