Automatically Push IP Address with Password-less SSH

Motivation

I have a Linux box in my office and I can access it from home with its public IP address. But the problem is that its IP address changes constantly as it is dynamically allocated. So I need a simple way to know the target system’s current IP address whenever I have to.

Here is how I setup a cron job to push the target machine’s public IP address to a common remote server, which my home computer also has access to, through password-less SSH. Something like this:

AutoPushIP

Setting Up

Step 1: SSH login without password on The Target Box

Obviously, the first step is to manually setup password-less SSH from the target machine, i.e. my office computer, to the common remote server. This step is only required to be done once. And I highly recommend password-less public key authentication to anyone, who needs to access a remote host. It is more convenient and more secure compare to password-based authentication.

The chance is that you may already have an existing SSH key. Checking for existing SSH keys to determine if you need to generate a new SSH key. If you do need a new key, here is a simple command to do just that:

# Generate a 4096-bits length RSA type key with comment "you@email.com"
ssh-keygen -t rsa -b 4096 -C "you@email.com"

Then, you just need to copy your public key to all the remote hosts you want to have access to. In our case, the host you definitely want to copy to is the common remote server. This is a really simply process with a utility called ssh-copy-id:

# example.com is the common remote host.
# yyang is my username on the server.
ssh-copy-id yyang@example.com

Step 2: The Script to Push The IP Address

Here is the Bash script push_ip_to_remote.sh, which contains one line of real code:

ifconfig | ssh yyang@example.com "cat > ~/Temp/ip.txt"

That’s it. Simple. Right?

This command use ifconfig to get the IP addresses, then pipe it to a file located on the remote server with a one-shot command through SSH.
Again, example.com is the common remote host that both my target office computer and my home system have access.
yyang is my username on the common remote host.
~/Temp/ip.txt is the file, on the common remote host, that holds the IP address information. You can put it anywhere you want on the remote host, just make sure all its parent folders exist.

Of course, you can make the script fancier with ideas, like[1]:

  • Only push the public IP address.
  • Check if IP address really changed before pushing.
  • Make the file hidden by default.

I don’t feel the needs, so let’s just keep it simple.

Step 3: Schedule a Cron Job on The Target Computer

Now, we want to add a cron job for the user whose key we used to setup password-less login in step 1.
First of all, you need to make sure the user has the right permission to run crontab. [3]
Next, use the -e option to edit the user’s crontab:

crontab -e

Then paste the following entry to the file:

PATH=/bin:/usr/bin:/sbin
0 * * * * "/path/to/script.sh"

This will add a cron job to run the given script at minute 0 of each hour.
/path/to/script.sh is the location of the push_ip_to_remote.sh script we created in step 2. And of course, you need to make sure it is executable by the user. [4]
/sbin is where ifconfig located on my Ubuntu and CentOS. By default, cron restricts the $PATH environment variable to /bin:/usr/bin, so we need to explicitly add /sbin to $PATH in order to run ifconfig. [5]

 

Reference

[1] Send Email when IP Address Changes.
[2] Bash – Dynamic Public IP Address Monitor Script.
[3] Crontab – Quick Reference.
[4] How to write a script to detect and notify of an IP address change.
[5] Why is my crontab not working, and how can I troubleshoot it?