How to log your IP Address

Have you you ever thought: "Mhmmm… was that me behind that IP address at this particular point in time? I wish I'd know that because THAT WOULD HELP ME A LOT TO DETERMINE IF I HAVE AN ISSUE HERE OR NOT!"

Bash script

#!/bin/bash

ipcur=$(curl -s ipecho.net/plain)
if ! tail -n 1 ~/.cronjobs/iplog.csv | grep -q "$ipcur"; then
    {
    printf '%s;' "$(date "+%Y-%m-%d;%H:%M")";
    curl -s ipinfo.io/json | jq -r '.ip + ";"
    + .hostname + ";"
    + .city + ";"
    + .region + ";"
    + .country + ";"
    + .loc + ";"
    + .org + ";"
    + .postal + ";"';
    } >> ~/.cronjobs/iplog.csv
fi

This bash script checks the current external IP address. If the result differs from the previous, last used IP address it appends a new line with the specified values to the csv logfile. Theses values are:

Date
Time
IP
Hostname
City
Region
Country
Location
Organisation
Postal Code

You may ask yourself why there's two services, ipinfo.io and ipecho.net?
Under certain circumstances, i.e. when you're on a mobile connection, I've found ipecho to be faster and it's only querying a single value (the IP address) compared to the json block of ipinfo which contains multiple values. Though I admit I'm not sure if that even matters.

time curl ipecho.net/plain;echo
0.00s user 0.00s system 0% cpu 2.317s total


❯ time curl -s ipinfo.io/104.161.79.83
{
  "ip": "104.161.79.83",
  "hostname": "No Hostname",
  "city": "Phoenix",
  "region": "Arizona",
  "country": "US",
  "loc": "33.4319,-112.0150",
  "org": "AS53755 Input Output Flood LLC",
  "postal": "85034"
}
0.00s user 0.01s system 0% cpu 2.572s total

Jealous of my latency? Probably not.

The Logfile

DATETIMEIPHOSTNAMECITYREGIONCCGPSORGANISATIONZIP
2017-01-0100:00104.161.79.83phoenix.crstin.comPhoenixArizonaUS33.4319,-112.0150AS53755 Input Output Flood LLC85034
2017-01-0208:458.8.8.8google-public-dns-a.google.comMountain ViewCaliforniaUS37.3860,-122.0838AS15169 Google Inc.9403
2017-01-0319:3067.215.92.218No HostnameMoragaCaliforniaUS37.8381,-122.1026AS36692 OpenDNS, LLC9455
2017-01-0410:30179.60.192.36edge-star-mini-shv-01-cdg2.facebook.comMenlo ParkCaliforniaUS37.4590,-122.1781AS32934 Facebook, Inc.9402
2017-01-0514:1517.172.224.47velocityengine.comCupertinoCaliforniaUS37.3230,-122.0322AS714 Apple Inc.9501
2017-01-0611:00213.186.33.5redirect.ovh.net  FR48.8582,2.3387AS16276 OVH SA 
2017-01-0717:30151.91.35.73No HostnameTurinPiedmontIT45.0705,7.6868AS12734 Fiat Information Technology, Excellence and Methods S.p.A.1015
2017-01-0813:45194.153.110.160No HostnameParisÎle-de-FranceFR48.9167,2.3833AS49566 COMMUNE DE PARIS9330

The above table is an illustration of what the logfile might look like.

CSV Headers

To enable csv headers, put this code on the first line of iplog.csv.

DATE;TIME;IP;HOSTNAME;CITY;REGION;COUNTRY;LOCATION;ORG;POSTAL;

Automate the script

Open crontab on the command line and add the cronjob in the editor.

❯ crontab -e
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                       7 is also Sunday)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *  command to execute

*/15 *  *  *  *  source ~/.cronjobs/iplog.sh

In this case iplog.sh will be executed every 15 minutes.

System Setup and Services Used: