64 lines
2.0 KiB
Bash
64 lines
2.0 KiB
Bash
#!/bin/bash
|
|
|
|
# Log file
|
|
log_file="/var/log/watchdog.log"
|
|
|
|
# Configuration file
|
|
config_file="/etc/watchdog.conf"
|
|
|
|
# Function to update watchdog configuration
|
|
update_watchdog_config() {
|
|
# Extract watchdog IP addresses
|
|
watchdog=$(sudo cat /var/lib/dhcp/* | grep -a "option watchdog" | tail -1 | \
|
|
awk '{for (i=3; i<=NF; i++) printf "%s ", $i}' | tr -d '";')
|
|
|
|
IFS=', ' read -r -a ip_array <<< "$watchdog" # Split IPs by comma or space
|
|
|
|
# Backup current configuration
|
|
sudo cp "$config_file" "$config_file.bak"
|
|
|
|
# Clean existing configurations
|
|
sudo sed -i '/ping/d' "$config_file"
|
|
sudo sed -i '/^interval/d' "$config_file"
|
|
|
|
# Set interval
|
|
echo "interval = 58" | sudo tee -a "$config_file" > /dev/null
|
|
|
|
if [ ${#ip_array[@]} -eq 0 ]; then
|
|
echo "No IP addresses found. Watchdog configuration cleared." >> "$log_file"
|
|
return 0 # No IP addresses to configure
|
|
else
|
|
# Insert IPs into config
|
|
for ip in "${ip_array[@]}"; do
|
|
echo "ping = $ip" | sudo tee -a "$config_file" > /dev/null
|
|
done
|
|
echo "Configured watchdog to ping: ${ip_array[*]}" >> "$log_file"
|
|
|
|
# Restart watchdog service
|
|
sudo systemctl restart watchdog
|
|
return 1 # IP addresses were configured
|
|
fi
|
|
}
|
|
|
|
while true; do
|
|
if update_watchdog_config; then
|
|
echo "IP addresses found and watchdog restarted. Exiting loop." >> "$log_file"
|
|
break
|
|
else
|
|
echo "No IP addresses found. Waiting for $interval seconds before checking again..." >> "$log_file"
|
|
fi
|
|
|
|
# Check time limit
|
|
if [ "$(date +%s)" -ge "$end_time" ]; then
|
|
echo "5 minutes have passed. Stopping the watchdog service." >> "$log_file"
|
|
sudo systemctl stop watchdog
|
|
break
|
|
fi
|
|
|
|
echo "Waiting for $interval seconds before checking for IP addresses again..." >> "$log_file"
|
|
sleep $interval
|
|
done
|
|
|
|
# Version 1.5:
|
|
# Created 2024 by Tim Eertmoed @ WiS IT-Solutions GmbH, Germany to work on Raspian as custom PXE init script.
|