Are you facing issues with your ppp0 interface losing its IP address? This can disrupt your network connectivity and create unnecessary downtime. Automate ppp0 interface monitoring and restart using a bash script combined with systemd and udev rules. This approach ensures that your ppp0 interface stays active and automatically recovers from disconnections.
Why Automate ppp0 Interface Monitoring and Restart?
The ppp0 interface, commonly used for point-to-point protocol connections, is prone to losing its IP address due to network interruptions. Manually restarting the interface can be tedious and may lead to prolonged downtime. By automating ppp0 interface monitoring and restart, you can minimize downtime and maintain a stable network connection.
Prerequisites
Before you begin, ensure you have:
- A Linux system with the
ppp0interface configured. - Root or sudo privileges.
Step 1: Create the Bash Script
Create a bash script to check if the ppp0 interface has an IP assigned. If not, it will restart the interface and keep checking every 2 minutes.
Script: /usr/local/bin/check_ppp0.sh
#!/bin/bash
# Function to check if ppp0 has an IP address
check_ip() {
ip_address=$(ip -4 addr show ppp0 | grep -oP '(?<=inets)d+(.d+){3}')
if [[ -z "$ip_address" ]]; then
echo "$(date) - IP Detected: NONE" | tee -a /var/log/ppp0_check.log
return 1 # No IP assigned
else
echo "$(date) - IP Detected: $ip_address" | tee -a /var/log/ppp0_check.log
return 0 # IP is assigned
fi
}
# Function to restart the ppp0 interface
restart_interface() {
echo "$(date) - No IP on ppp0. Restarting interface..." | tee -a /var/log/ppp0_check.log
ifdown ppp0
sleep 5
ifup ppp0
}
# Check if ppp0 has an IP assigned
if ! check_ip; then
restart_interface
# Wait for 2 minutes (120 seconds) and check if IP is assigned
for ((i=1; i<=24; i++)); do # 24 checks with a 5-second sleep between them
sleep 5
if check_ip; then
echo "$(date) - IP assigned to ppp0 after restart." | tee -a /var/log/ppp0_check.log
exit 0
fi
done
# If no IP assigned after 2 minutes, continue checking indefinitely
echo "$(date) - Still no IP on ppp0. Continuing to check every 2 minutes." | tee -a /var/log/ppp0_check.log
while true; do
sleep 120 # Wait for 2 minutes
if ! check_ip; then
echo "$(date) - No IP on ppp0 after 2 minutes. Restarting interface again..." | tee -a /var/log/ppp0_check.log
restart_interface
fi
done
else
echo "$(date) - IP is assigned to ppp0. No action needed." | tee -a /var/log/ppp0_check.log
fiMake the script executable:
sudo chmod +x /usr/local/bin/check_ppp0.shStep 2: Create a systemd Service for ppp0 Monitoring
To automate ppp0 interface monitoring and restart, create a systemd service to run the script as a background process.
Service File: /etc/systemd/system/check_ppp0.service
[Unit]
Description=Check and Restart ppp0 Interface if no IP is Assigned
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/check_ppp0.sh
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetReload systemd, enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable check_ppp0.service
sudo systemctl start check_ppp0.serviceStep 3: Create a udev Rule for ppp0 Interface
To enhance automation, add a udev rule that restarts the service when the ppp0 interface is disconnected.
udev Rule: /etc/udev/rules.d/99-ppp0-monitor.rules
ACTION=="remove", SUBSYSTEM=="net", KERNEL=="ppp0", RUN+="/bin/systemctl restart check_ppp0.service"Reload udev rules:
sudo udevadm control --reloadStep 4: Monitor Logs and Test the Setup
The script logs all activities to /var/log/ppp0_check.log. Monitor the logs with:
tail -f /var/log/ppp0_check.logTo test the setup, manually disconnect ppp0:
sudo ifdown ppp0The script should automatically detect the disconnection and attempt to restart the interface.
Conclusion
By automating ppp0 interface monitoring and restart, you ensure continuous connectivity and minimize network downtime. This solution efficiently handles IP loss scenarios by repeatedly checking the ppp0 interface and restarting it as needed. Integrating the bash script with systemd and udev rules provides a robust and reliable network management solution.


