Logging Global IP address

Date: 2023/12/24 (initial publish), 2024/02/23 (last update)

Source: en/note-00060.md

Previous Post Top Next Post

TOC

Here is a memo over IP address and its logging.

Local IP address

Local IP address can be obtained by using ip addr.

 $ ip addr

For processing this output with script, use of -o option is handy.

Global IP address

If a workstation is behind the firewall, Global IP address is that for the gateway device.

Googling “what is my IP” gives lots of sites offering Global IP verification.

Here are interesting articles on this topic.

As I checked my IP connection situation:

So I don’t get global IP with IPv4 but have global IP with IPv6.

Logging IP address with systemd timer

~/.config/systemd/user/myip.service

[Unit]
Description=Check external IP address
Documentation=man:myip(1)
# journalctl -a -b -t myip

[Service]
Type=oneshot
Nice=15
# ifconfig.co can be other IP servicing similar functionality.
# See https://serverfault.com/questions/1007184/how-to-check-ipv6-address-via-command-line
ExecStart=systemd-cat -p 5 -t "myip" sh -c 'echo "External IP: IPv4=$(curl -4 https://ifconfig.co 2>/dev/null)"'
ExecStart=systemd-cat -p 5 -t "myip" sh -c 'echo "External IP: IPv6=$(curl -6 https://ifconfig.co 2>/dev/null)"'
IOSchedulingClass=idle
CPUSchedulingPolicy=idle
StandardInput=null
StandardOutput=null
StandardError=null

~/.config/systemd/user/myip.timer:

# activate by: systemctl --user enable myip.timer
[Unit]
Description=Check external IP address
Documentation=man:myip(1)

[Timer]
# 90 sec min is enough for connecting to new Wifi service
OnStartupSec=90

[Install]
WantedBy=timers.target

Activating timer service

 $ systemctl --user enable myip.timer

This creates a symlink ~/.config/systemd/user/timers.target.wants/myip.timer pointing to ~/.config/systemd/user/myip.timer

Checking logged IPs

IP address as I powered-on this time.

 $ journalctl -a -b -t myip

IP address as I powered-on recently.

 $ journalctl -a -t myip
Previous Post Top Next Post