Logging IP Address Changes

Chat about just about anything else
Forum rules
Do not post support questions here. Before you post read the forum rules. Topics in this forum are automatically closed 30 days after creation.
Locked
WhatUsernameIsFree?

Logging IP Address Changes

Post by WhatUsernameIsFree? »

Hi all,

is there a way to log the IP address that my router has? I'm looking to see if I have a dynamic IP address, or a static IP address. I'd like to see if I can find the history of my IP address too. Does this forum keep a record of them somewhere?

Thanks :)
Last edited by LockBot on Wed Dec 07, 2022 4:01 am, edited 1 time in total.
Reason: Topic automatically closed 30 days after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Logging IP Address Changes

Post by xenopeek »

I've sent you a message with some information. Usually your Cable/DSL modem has a dynamic IP address--which it keeps till you power it off or reset it. Then it will get a new IP address from your ISP. You may be able to log in to the Cable/DSL modem through a web configurator, and possibly you can access logging about this there.
Image
WhatUsernameIsFree?

Re: Logging IP Address Changes

Post by WhatUsernameIsFree? »

Hi,

With a bit of digging, I found I can use crobtab to check my IP and write it to a file with the following.

Code: Select all

wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' > file.txt
But this seems just keep overwriting the file rather than adding more information to it. How can I make it add to the file rather than overwrite it?

Thanks
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Logging IP Address Changes

Post by xenopeek »

Change the "> file.txt" at the end to ">>file.txt". > is overwrite, >> is append.

And I couldn't resist automating things. Following script will check your IP daily, and if it has been changed will log the new IP with timestamp in /var/log/checkip.log. Create a file called checkip with the following content:

Code: Select all

#!/bin/bash

# redirect stdout and stderr to logfile
exec >>/var/log/checkip.log 2>&1

# check IP against previous IP
CURRIP=$(wget -q -O - checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
if [[ ! -f /var/log/checkip.log ]]; then
	echo "$(date +'%Y-%m-%d %H:%M'): $CURRIP"
else
	LASTIP=$(tail -n 1 /var/log/checkip.log | sed 's/^.*: //')
	if [[ "$LASTIP" != "$CURRIP" ]]; then
		echo "$(date +'%Y-%m-%d %H:%M'): $CURRIP"
	fi
fi
Make the file executable:

Code: Select all

chmod +x checkip
Then copy the file so that it will be run daily:

Code: Select all

sudo cp checkip /etc/cron.daily
Image
WhatUsernameIsFree?

Re: Logging IP Address Changes

Post by WhatUsernameIsFree? »

Hi,

Wow :shock:

Looks good, I'll give it a go! :)
ablueman

Re: Logging IP Address Changes

Post by ablueman »

Hmm .. I think his is better than mine looks a lot tidier but here is mine for comparison :)
Only real different is that mine uploads the file using ncftp to my homepage space where i can then use the IP address as a variable.

Code: Select all

#!/bin/bash 

# Check IP address compare to see if its changed.
# Save the file to a couple of different places locally
# FTP the file to regular hosting.

# File to keep IP address in
IP_FILE=file1.txt
	echo "File to store and compare IP:" $IP_FILE

# wget to check current IP address.
IPADDRESS=$(wget www.ablueman.co.uk/ip.php -O - -q)
	echo "New IP:" $IPADDRESS

# Read the previous IP address from file
source $IP_FILE
	echo "Old IP:" $OLD_IP

# Compare the new to the old IP
if [ "$IPADDRESS" != "$OLD_IP" ]
	then
        echo "New IP address detected: $IPADDRESS"

		# Write new address to file
        `echo "OLD_IP=$IPADDRESS" > $IP_FILE`
		
		# FTP File to the appropriate places
		# Store it locally as well

		ncftpput -u user -p pass webspace.co.uk /remotefiletostorein/ /local/file/tostore.php
		
fi
WhatUsernameIsFree?

Re: Logging IP Address Changes

Post by WhatUsernameIsFree? »

Hi,

Just doing this for my laptop and I'm getting an error when I copy and paste Xenopeek's code

The /var/log/ file just contains the following.

Code: Select all

checkip: 8: checkip: [[: not found
checkip: 12: checkip: [[: not found

Code: Select all

    #!/bin/bash

    # redirect stdout and stderr to logfile
    exec >>/var/log/checkip.log 2>&1

    # check IP against previous IP
    CURRIP=$(wget -q -O - checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
    if [[ ! -f /var/log/checkip.log ]]; then
       echo "$(date +'%Y-%m-%d %H:%M'): $CURRIP"
    else
       LASTIP=$(tail -n 1 /var/log/checkip.log | sed 's/^.*: //')
       if [[ "$LASTIP" != "$CURRIP" ]]; then
          echo "$(date +'%Y-%m-%d %H:%M'): $CURRIP"
       fi
    fi
Thanks
Locked

Return to “Open Chat”