Creating a cron job for IPset involves scheduling a task that updates IPset with a list of IP addresses at specified intervals. In this tutorial I will show you how to update a

Update the syntax in bold and italic with your own.

Here’s a step-by-step guide on how to set up a cron job for IPset:

  1. Create an IPset: Before setting up a cron job, create the IPset with the list of IP addresses you want to maintain. You can use the ipset command to create the set. For example:
   sudo ipset create demolist hash:ip

Replace myipset with your desired set name.

  1. Create a Script: Create a script that adds or updates the IPset with your desired list of IP addresses. You can use a simple text file containing the IP addresses or use other methods to retrieve the list, like downloading a file from the internet. Here’s an example script (e.g., update_ipset.sh) that adds IP addresses from a file:
#!/bin/bash

# Find IP address and store it in $ip
ip=$(dig +short demo.dyn.org)

ipset flush demolist
ipset add demolist $ip

# For quick validation
output=/tmp/demo.dyn.org.txt
echo $ip >> $output

Make sure to customize the script with your IPset name and the path to your IP list file.

  1. Make the Script Executable: Ensure that your script is executable by running:
   chmod +x update_ipset_demolist.sh
  1. Set up the Cron Job: Open your crontab configuration by running:
   crontab -e

Add a line to schedule your script to run at your desired interval. For example, to run it every min everyday:

   0 * * * * /path/to/your/update_ipset_demolist.sh

This line represents the schedule in cron format (minute, hour, day of the month, month, day of the week), followed by the path to your script.

  1. Save and Exit: Save your crontab configuration and exit the text editor.

Your cron job is now set up to periodically update your IPset with the IP address. Make sure to replace /path/to/your/update_ipset_demolist.sh with the actual path.

Remember to test your script and cron job to ensure that they work as expected. You can manually run the script to verify that it adds the IP addresses to your IPset.

Some useful commands for checking the cron jobs.

sudo systemctl status cron
grep cron /var/log/syslog