Get public IP with reliable service

Hi tech family, I'm in need of your help once again. I've set up a Raspberry Pi with an LCD that shows detailed info about the system and constantly switches between different info on the screen. I need to show the external IP since it's not static and, without realizing it, I tripped the ipinfo.io DDoS limit making those continuous requests. Now I've found a new service called ipify.org that allows a lot of requests but I'm getting frequent SSL errors that throw my python script off.
How can I update constantly my ip without making tons of requests and reliably from a service of some kind?
If you need to take a look at the code I wrote I'll provide it but I don't know where to host it so just you guys can see it.
Thanks in advance for all the help.

One way to do it would be to use something like curl to login to your routers admin page and pull the WAN ip from it or if you use something like pfSense you can get the script to login via SSH and pull the ip with a shell command or even pipe it using netcat.

1 Like

If you have DynDNS set up in your router, you could resolve your DynDNS domain and get your public IP that way.

1 Like

Thanks for the answers guys!

@MichaelLindman Wait I didn't understand exactly what you mean when you say log into my router and get the IP that way. The IP in my router is in a sub menu of the GUI so I don't know how to navigate to that with a simple curl. I don't have a PfSense router unfortunately. With that thing would've been a piece of cake to get.

@comfreak Would work with noIP too I guess.

Tonight I woke up and though "eureka" because I think I've found a way but I don't know how to implement it: what if I do a request once every two hours (or even more), write down the IP onto a file and read the IP from that file? I would be doing far less requests and not trigger and DDoS firewall. What do you think?

You can use curl to trawl web pages, can even login to them and stuff, shouldn't be too difficult to make a script to login to the admin page and find the ip address that way.

1 Like

Even navigate through the GUI? Also if it's in a GUI how can I "tell" the script to take only that number?

Sort of, you login with a curl command and save the cookies and load them with a different curl command for the specific page you want to access, this page on stackoverflow has some details on logging in with curl:

also curl pulls the raw html so you'd use stuff like grep/sed/regex to find it and cut to remove the other unwanted stuff.

Can't you just ssh to it and parse the output of ifconfig or ip addr?

yes, that is what I stated in my earlier reply if the OP was using a router which has SSH such as pfSense but most consumer routers do not allow SSH access.

I can't unfortunately, I just checked the specs and SSH is out of question.

To be honest i thought this was what you did to begin with. So, how many times per second does it pull the IP now? It must do it constantly. Anyway, if you can pull a sinlge ip from a text file in your current script, just do the ip command from wherever and put it in a textfile overwriting what is already there. Like so for instance.

curl ipinfo.io/ip > ~/ip

If you do this once per hour I don't think you'll get blocked for DoS'ing.

1 Like

I didn't do that, I just used ip = wget [site I don't remember atm].readline()
return(ip)
. All of this in a while cycle with a counter of 10secs. This caused the trigger of the DDoS protection.
Now I just need to figure out how to write everything into that script in order to pull the IP from the site, write it onto a .txt file and update it ever "x" hours.

Can't you just set it to pull every hour or so, instead of every 10sec?

I can't because the LCD shows a different screen every 10 seconds with different details and in one I'm showing my IP. I hope I explained myself well enough

1 Like

Sure, all you need is some way to get your public IP address using DNS. Since your DNS resolver will cache the request, it won't even leave your network until the cache expires. Usually dynamic DNS records have a short time of 5 minutes or so, so if your IP changes, it shouldn't be a big deal.

This is the answer.

Can't you store it as a variable, that's ridiculously inefficient.

2 Likes

If you are looking for something to fire off the text file update script, use cron.

But I think "Runit" has the ability to schedule the running of programs too. You will have to look up how exactly to do it.

1 Like

@Eden I was but that variable was getting updated really really often by a timed while cycle that lasts 10 seconds, as I said before.

@DeusQain In the end I did used cron to run wget http://ipinfo.io/ip -qO - > ext_ip.txt every 6h and on every reboot. Than I just pick that information up from the text file and I'm done. I don't know if that's inefficient or not but I don't know any other way to avoid getting my IP flagged for DDoSing again from that service.

Thanks to both of you for the answers, I appreciate it!

1 Like

Did you program it? The inefficiency still is in the polling of the ip (from text file or ipinfo.io) when its not needed. If you have the time you should look at changing it to only poll and process the data is it needs to, and skip and use the old data if its still current.

1 Like

Yeah, that part was done entirely by me like 90% of the whole program. It's my first time programming in Python and has been a bit of a rough start. If I post the code somehow can I get some feedbacks?

P.S. I could use those improvements to improve also the polling of other informations that are slowing the whole script down a bit. For example the clock and uptime timer are lagging a bit because I'm polling data from a DHT11 temp and humidity sensor I think.
Or the issue might be that I'm using psutil (python lib for sys info) for those timers?