IP-logger - Part 3: create directory, copy scrip, edit script, test script

In this part we will create a directory, copy the script, edit the script and test the script

Step 1: Create directory

Create a directory in a directory where you want to place the script.

On SSH you can nivate to the directory where you want to create the directory for the IP-logger script and execute:

mkdir ip-logger

In my case I created the directory at /data/local, so the whole path for the IP-logger directory would be /data/logger/ip-logger. From now on I will use this example path.

Step 2: Copy script

Copy the script to the directory. On Windows WinSCP is the easiest way to get the script copied to the right location.

You can find the script within the attachments on the bottom of this page. You may freely edit the script to suit your whishes and needs. I would be happy when you inform me about the changes you made!

Based on the example path above the whole path to the script will be /data/local/ip-logger/ip-logger.sh.

Step 3: Edit script variables

You will need to edit some variables within the script.

It's best to edit the script with the built-in editor of WinSCP or on a SSH session with Nano.

You can install Nano with the command

opkg install nano

You can open Nano with the script by execute the following command when you are at the correct directory where the IP-logger script is being placed (with the example path that would be /data/local/ip-logger):

nano ip-logger.sh

All variables are defined at the top of the script. They are explained on this section. All variables are defined between quotes except IPLOGGER_ENABLELOG.

  • IPLOGGER_SERVERNAME="My OpenWRT router";
    • Here you can define the name you will see at your pushmessages and/or e-mails. It's advisable to use an IP-address or hostname, but you are free to use any text you want. This variable is mandatory.
  • IPLOGGER_LOGFILE="ip-logger.log";
    • The location of the logfile. It is only used when logging is enabled and set to a file. It can be absolute path (with the example path that would be /data/local/ip-logger/ip-logger.log) or relative to the working directory (for example only ip-logger.log). This variable is mandatory when logging has been set to enabled (see below).
  • IPLOGGER_IPFILE="ip-logger.txt";
    • The location where the scripts records the last IP-address. This variable is mandatory. It can be absolute path (with the example path that would be /data/local/ip-logger/ip-logger.txt) or relative to the working directory (for example only ip-logger.txt).
  • IPLOGGER_IPTEMPFILE="ip-logger-temp.txt";
    • The location where the scripts temporarely records the current IP-address. This variable is mandatory.. It can be absolute path (with the example path that would be /data/local/ip-logger/ip-logger-temp.txt) or relative to the working directory (for example only ip-logger-temp.txt). This file will be written during execution of the script and automatically removed within a few seconds after it has been written.
  • IPLOGGER_ENABLELOG=1;
    • Enable or disable logging of all actions of the script. Set to 1 to enable logging or set to 0 (zero) to disable logging. This variable is mandatory.
  • IPLOGGER_LOGOUTPUT="syslog";
    • This variable is mandatory when logging is set to enabled.
    • You can choose from 3 options: screen, file and syslog.
      • When you choose screen all actions of the script will be shown on your SSH session. This can be very handy when editing and testing the script.
      • When you choose file all actions of the script will be written to the file set at IPLOGGER_LOGFILE. This can be handy to check the correct processing of the script when running in crontab on the background and you want to read the log from another script or webpage.
      • When you choose syslog all actions of the script will be written to the OpenWRT's system log. You can view the logs with logread or when you only want to see all logs of IP-logger: logread | grep "IP-logger".
  • IPLOGGER_PUSHBULLET_TOKEN="123456789abcdefgh";

Step 4: Edit sending of e-mails to your whiches

At the bottom there are a few lines which you have to comment out or edit.

Just below the line

function pushbullet_inform_new {

you will find the following 3 lines:

# Comment the following 2 lines if you don't want to use a second pushmessage to an e-mailaddress. Otherwise change the e-mailaddress to the one you want to send the pushmessage to.

log "Sending e-mail using Pushbullet.";

curl -k --silent --header "Access-Token: $IPLOGGER_PUSHBULLET_TOKEN" --header "Content-Type: application/json" --data-binary "{\"title\":\"IP-adress of $IPLOGGER_SERVERNAME changed\",\"body\":\"The IP-adress of $IPLOGGER_SERVERNAME has changed from $GETOLDEXTIP to $GETEXTIP at $(date +"%m-%d-%Y %H:%M:%S").\",\"type\":\"note\",\"email\":\"youre-mailaddress@goes.here\"}" --request POST https://api.pushbullet.com/v2/pushes > /dev/zero;

If you want top recieve an e-mail regarding the change of the external IP-address you can leave this 3 lines in tact. The only thing you have to do is changing the phrase youre-mailaddress@goes.here into your own e-mail address which isn't in use as a Pushbullet account (otherwise you will recieve a pushmessage twice).

Hint: GMail has the option to add "+yourtag" afther the prefix of your e-mail address. So if your e-mail address is mymailaddress@gmail.com you could use mymailaddress+iplogger@gmail.com to recieve the e-mails on and to avoid delivering as pushmessage. Both mailaddresses redirects to the same mailbox. You can even add rules to automatically filter and add labels to the mails.

If you don't want to recieve e-mails you have to add a square (#) before the two lines who doesn't have a square at the beginning of the lines.

Repeat above instruction for the same 3 lines just below the line

function pushbullet_inform_updated {

Step 5: Provide execute rights to the script

To be able to execute the script it needs execute rights.

You can provide the execute rights by executing the command:

chmod +x ip-logger.sh

from the directory where the script is placed. With the example path that would be /data/local/ip-logger.

Step 6: Test script

Now the script is ready to test.

From the IP-logger directory (with the example path that would be /data/local/ip-logger) you can execute the script with the following command:

./ip-logger.sh

When you made a typo bash will inform you on which line contains the error. When in doubt how to solve the error you can contact me at j [dot] maathuis+iploggercontact [at] gmail [dot] com (replace "[character]" by its corresponding character without braces) and maybe I can help you out, but I can not provide any guarantees on that.

  • When you did enable logging and set logging to the screen you will see what the script does right on your SSH session.
  • When you did enable logging and set logging to a file you can read the IP-logger file with a text editor.
  • When you did enable logging and set the logging to syslog you can see the IP-logger actions with the command logread | grep "IP-logger".
  • When you did install the Pushbullet client on your Windows desktop you would see something like this in the bottom right corner:
  • When you did install the Pushbullet client on your Android smartphone you would see something like this in your notification area:

When you don't see any strange behaviour you did install the script correctly.

Step 7: Install the cronjob

Downloads

Below are the files included with this projectpage.