Logging IP Address Changes

Chat about just about anything else

Logging IP Address Changes

Postby WhatUsernameIsFree? on Wed Jan 09, 2013 7:23 am

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 :)
WhatUsernameIsFree?
Level 4
Level 4
 
Posts: 216
Joined: Wed Oct 19, 2011 1:32 pm

Linux Mint is funded by ads and donations.
 

Re: Logging IP Address Changes

Postby xenopeek on Wed Jan 09, 2013 8:38 am

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.
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13708
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Logging IP Address Changes

Postby WhatUsernameIsFree? on Thu Jan 10, 2013 4:39 am

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
WhatUsernameIsFree?
Level 4
Level 4
 
Posts: 216
Joined: Wed Oct 19, 2011 1:32 pm

Re: Logging IP Address Changes

Postby xenopeek on Thu Jan 10, 2013 5:16 am

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
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13708
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Logging IP Address Changes

Postby WhatUsernameIsFree? on Thu Jan 10, 2013 10:48 am

Hi,

Wow :shock:

Looks good, I'll give it a go! :)
WhatUsernameIsFree?
Level 4
Level 4
 
Posts: 216
Joined: Wed Oct 19, 2011 1:32 pm

Re: Logging IP Address Changes

Postby ablueman on Fri Feb 08, 2013 10:09 am

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
ablueman
Level 1
Level 1
 
Posts: 1
Joined: Fri Feb 08, 2013 9:56 am

Re: Logging IP Address Changes

Postby WhatUsernameIsFree? on Sun Mar 24, 2013 8:30 am

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
WhatUsernameIsFree?
Level 4
Level 4
 
Posts: 216
Joined: Wed Oct 19, 2011 1:32 pm

Linux Mint is funded by ads and donations.
 

Return to Open chat

Who is online

Users browsing this forum: No registered users and 7 guests