5

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?

4 Answers4

4

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

$ curl ipinfo.io/ip
cuongnv23
  • 184
  • 5
2

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]
        f.close()
    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 08:32
  • while you do f.close() , closing the file, in the if os.path.exists(LOG): condition, you forget to in the if ip != last_ip: condition. – barlop Jun 26 '22 at 03:00
0

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

Nathan
  • 205
0

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.

Jelu
  • 1
  • 1