A simple netcat ‘pong’ service
Given you have a machine outside your home LAN, you can create a very simple ‘pong’ service to get your external IP address. For example if you have to update a dynamic DNS entry for a server which is running inside your LAN.
I just call it ‘pong’, opposed to the wellknown ‘ping’ tool.
It simply uses netcat (you might have to install it first) to listen (-l) on a given port (-p) and dump its verbose (-v) output into a file (this will contain the IP address of the incoming request). Then extract the IP address and return it. Then wait for the next request.
On the external machine
Create the file /usr/local/bin/pong.sh
#!/bin/bash
port=12345
_term() {
# Catch the interrupt signal to enable
# a graceful exit
fuser -k $port/tcp
exit 0
}
trap _term INT
while true
do
file=/tmp/$RANDOM.ip
nc -lvp $port -c "grep connect $file | cut -d'[' -f 3 | cut -d']' -f 1" 2> $file
rm $file
done
Create a systemd service /etc/systemd/system/pong.service
[Unit]
Description=Pong Service
After=network.target
[Service]
User=nobody
WorkingDirectory=/tmp
ExecStart=/usr/local/bin/pong.sh
[Install]
WantedBy=multi-user.target
Start it:
systemctl start pong
Respectively enable it at boot time:
systemctl enable pong
On your local machine:
Get your external IP address by running
nc [SERVER IP] 12345