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