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
DATE | TIME | IP | HOSTNAME | CITY | REGION | CC | GPS | ORGANISATION | ZIP |
---|---|---|---|---|---|---|---|---|---|
2017-01-01 | 00:00 | 104.161.79.83 | phoenix.crstin.com | Phoenix | Arizona | US | 33.4319,-112.0150 | AS53755 Input Output Flood LLC | 85034 |
2017-01-02 | 08:45 | 8.8.8.8 | google-public-dns-a.google.com | Mountain View | California | US | 37.3860,-122.0838 | AS15169 Google Inc. | 9403 |
2017-01-03 | 19:30 | 67.215.92.218 | No Hostname | Moraga | California | US | 37.8381,-122.1026 | AS36692 OpenDNS, LLC | 9455 |
2017-01-04 | 10:30 | 179.60.192.36 | edge-star-mini-shv-01-cdg2.facebook.com | Menlo Park | California | US | 37.4590,-122.1781 | AS32934 Facebook, Inc. | 9402 |
2017-01-05 | 14:15 | 17.172.224.47 | velocityengine.com | Cupertino | California | US | 37.3230,-122.0322 | AS714 Apple Inc. | 9501 |
2017-01-06 | 11:00 | 213.186.33.5 | redirect.ovh.net | FR | 48.8582,2.3387 | AS16276 OVH SA | |||
2017-01-07 | 17:30 | 151.91.35.73 | No Hostname | Turin | Piedmont | IT | 45.0705,7.6868 | AS12734 Fiat Information Technology, Excellence and Methods S.p.A. | 1015 |
2017-01-08 | 13:45 | 194.153.110.160 | No Hostname | Paris | Île-de-France | FR | 48.9167,2.3833 | AS49566 COMMUNE DE PARIS | 9330 |
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
- macOS 10.12 Sierra
- bash
- crontab
- ipinfo.io and ipecho.net