Page 1 of 1

Logging IP Address Changes

Posted: Wed Jan 09, 2013 7:23 am
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 :)

Re: Logging IP Address Changes

Posted: Wed Jan 09, 2013 8:38 am
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.

Re: Logging IP Address Changes

Posted: Thu Jan 10, 2013 4:39 am
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

Re: Logging IP Address Changes

Posted: Thu Jan 10, 2013 5:16 am
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

Re: Logging IP Address Changes

Posted: Thu Jan 10, 2013 10:48 am
by WhatUsernameIsFree?
Hi,

Wow :shock:

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

Re: Logging IP Address Changes

Posted: Fri Feb 08, 2013 10:09 am
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

Re: Logging IP Address Changes

Posted: Sun Mar 24, 2013 8:30 am
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