🌐

Tutorial: Creating a Tailscale Ping and Restart Script

 
In this tutorial, you will learn how to create a shell script that pings a specified Tailscale client IP address. If the script cannot reach the IP, it will restart the Tailscale service. The IP address is configurable, allowing you to monitor different Tailscale clients as needed.

Prerequisites

  • A pfSense installation with terminal access.
  • The Tailscale service installed and running.
  • Basic knowledge of using the terminal and shell scripts.

Step 1: Create the Shell Script

  1. Open a terminal on your pfSense box.
  1. Create a new shell script file using the vi editor:
    1. sh Copy code vi /usr/local/bin/ping_tailscale.sh
  1. Insert the following script into the file:
    1. sh Copy code #!/bin/sh # Configurable Tailscale client IP address IP="PUT IN YOUR IP HERE" # Change this to the IP of your desired Tailscale client # Check if the specified IP is reachable if ping -c 1 $IP > /dev/null 2>&1 then echo "Ping successful. No action needed." else echo "Ping failed. Restarting Tailscale service." service tailscaled restart fi
  1. Make the script executable:
    1. sh Copy code sudo chmod +x /usr/local/bin/ping_tailscale.sh

Step 2: Make the IP Configurable

To change the IP address to monitor, simply edit the script:
  1. Open the script again:
    1. sh Copy code vi /usr/local/bin/ping_tailscale.sh
  1. Modify the line IP="PUT IN YOUR IP HERE" to reflect the desired Tailscale client IP address.
  1. Save and exit by pressing Esc, typing :wq, and hitting Enter.

Step 3: Set Up the Cron Job

To run the script automatically every hour, set up a cron job:
  1. Install the Cron Package (if not already installed):
      • Go to System > Package Manager in the pfSense web interface.
      • Click on the Available Packages tab.
      • Search for and install the Cron package.
  1. Configure the Cron Job:
      • Go to Services > Cron.
      • Click Add to create a new cron job.
      • Configure the fields as follows:
        • Minute: 0
        • Hour: /1 (to run at the top of every hour)
        • Day of Month:
        • Month:
        • Day of Week:
      • Command: Enter:
        • sh Copy code /usr/local/bin/ping_tailscale.sh
      • Click Save, then Apply Changes.

Conclusion

You have successfully created a script to ping a Tailscale client IP address and configured it to restart the Tailscale service if unreachable. The IP address can easily be modified to monitor different clients. This automation helps ensure that your Tailscale network remains operational.
Feel free to adapt the script further to suit your needs!