I'd like to save my public IP address to a log file so I can use them to exclude my own visit to my websites in the stats collections. At the moment I can see my actual public IP address--whatsmyip.org--but I believe that every time I off the modem, it changes.

I don't have a static public IP address, and I think there isn't a fixed range of IP that my ISP is giving me.

I'm running Linux Mint 17.3, is there any way that I've already a similar log file? If not, can I track my future IPs and how?


This one will give you your public IP, remove /ip part to see more info.

$ curl ipinfo.io/ip
  • It doesn't show my IP history, though. @cuongnv
    – overkill22
    Apr 21 '16 at 2:25
  • of course, it's not a full solution for you. But with it, you can write your own script, put that script in cron to make it get the IP and save into your log file.
    – cuongnv23
    Apr 21 '16 at 10:43

You can try to use some dynamic dns services like noip.com Then You can access resources by dns name, which will changing according to Your ip.

Generally, Your provider may NAT with pool of addresses. And every curl https://ipinfo.io/ip request will return a random address from this pool, depends on the settings of the NAT.

It's better to use different methods to track visits to web-site. F.e. cookies.

Here is a small python code to put in cron and collect addresses:

#!/usr/bin/env python

from datetime import datetime
import os
import requests

LOG = '/tmp/ip.log'
URL = 'https://ipinfo.io/ip'

r = requests.get(URL)
if r.status_code == 200:
    ip = r.content.decode('ascii').rstrip('\n')
    last_ip = None
    if os.path.exists(LOG):
        f = open(LOG, 'r')
        last_ip = f.readlines()[-1].split()[-1]
    if ip != last_ip:
        f = open(LOG, 'a')
        f.write("{} {}\n".format(datetime.now(), ip))
  • Any practical example of how a dynamic-DNS service would allow OP to correlate a two week-old log entry from xxx.xxx.xxx.xxx with his IP address at that time?
    – techraf
    Apr 20 '16 at 8:32

Add this line to your crontab file (crontab -e) to write the date and your public IPv4 address to a file called ip_public.txt, saved in your home directory, every day at 12:00 AM.

0 0 * * * echo $(date +\%Y-\%m-\%d) $(curl https://ipinfo.io/ip 2>/dev/null) >> ~/ip_public.txt 2>&1


Already an answer here, but this is another way of getting your public IP address, using DNS.

dig +short myip.opendns.com @resolver1.opendns.com

You would then have to wrap it in a script, to save the history.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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