HOWTO: Automatic PPPOE connection reset in case of packet loss



  • I've modified the script from @BennTech (https://forum.pfsense.org/index.php?topic=51714.msg276614#msg276614) to do the following:

    • Automatic PPPOE connection reset in case of packet loss

    • Own interface down and up sound (only played between 08am-10pm)

    • Separate e-mail notification for a successfully restored connection

    • Using the Systemlog

    It helps when the connection seems to be up and running but no data packets are going through.

    
    #!/bin/sh
    
    #=====================================================================
    # pingtest.sh, v1.0.2
    # Created 2009 by Bennett Lee 
    # Modified by s0nic
    # Released to public domain
    #
    # (1) Attempts to ping several hosts to test connectivity.  After
    #     first successful ping, script exits.
    # (2) If all pings fail, resets interface and retries all pings.
    # (3) If all pings fail again after reset, then reboots pfSense. (currently deactivated)
    #
    # History
    # 1.0.2   Added interface status sound, mail notification, systemlog and PPPOE reset. Disabled reboot.
    # 1.0.1   Added delay to ensure interface resets (thx ktims).
    # 1.0.0   Initial release.
    #=====================================================================
    
    #=====================================================================
    # USER SETTINGS
    #
    # Set multiple ping targets separated by space.  Include numeric IPs
    # (e.g., remote office, ISP gateway, etc.) for DNS issues which
    # reboot will not correct.
    ALLDEST="google.com 8.8.8.8"
    
    # Interface to reset, usually your WAN
    BOUNCE=WAN
    
    # Log file
    LOGFILE=/root/pingtest.log
    
    ZEIT=$(date +%k%M)
    #=====================================================================
    COUNT=1
    
    while [ $COUNT -le 2 ]
    do
    
    	for DEST in $ALLDEST
    	do
                    echo "PINGTEST: Pinging $DEST" | logger
    		ping -c1 $DEST >/dev/null 2>/dev/null
    
    		if [ $? -eq 0 ]
    		then
    			echo "PINGTEST: Ping $DEST OK." | logger
    			exit 0
    		fi
    	done
    
    	if [ $COUNT -le 1 ]
    	then
    		echo "PINGTEST: All pings failed. Resetting interface $BOUNCE." | logger  
    
    		# Interface RESET
    		/usr/local/sbin/pfSctl -c 'interface reload wan' >/dev/null 2>&1
    
    		# Interface DOWN
    		#/sbin/ifconfig $BOUNCE down | logger
    
    		# Notification
    		if [ "$ZEIT" -ge 800 ] && [ "$ZEIT" -le 2200 ]
    		then
    			/usr/local/bin/beep -p 1000 25
    			/usr/local/bin/beep -p 900 25 
    			/usr/local/bin/beep -p 800 25 
    			/usr/local/bin/beep -p 700 25 
    			/usr/local/bin/beep -p 600 25 
    			/usr/local/bin/beep -p 500 25 
    			/usr/local/bin/beep -p 400 25 
    			/usr/local/bin/beep -p 300 25 
    			/usr/local/bin/beep -p 200 50  
    		fi
    
    		# Give interface time to reset before bringing back up
    		#sleep 10
    
    		# Interface UP
    		#/sbin/ifconfig $BOUNCE up | logger
    
    		# Give WAN time to establish connection
    		sleep 15
    
    		# Notifications
    		for DEST in $ALLDEST
    		do
    			ping -c1 $DEST >/dev/null 2>/dev/null
    		done
    			if [ $? -eq 0 ]
    			then
    			MOMENT=`date +%d.%m.%Y@%H:%M:%Sh`
    			printf "PINGTEST: Interface $BOUNCE has been successfully resetted.\n\nReconnect: $MOMENT" | /usr/local/bin/php -q -c"/usr/local/etc/php.ini" /usr/local/bin/mail.php -s"pfSense.name - Reconnect"
    			echo "PINGTEST: Interface $BOUNCE has been successfully resetted." | logger 
    				if [ "$ZEIT" -ge 800 ] && [ "$ZEIT" -le 2200 ]
    				then
    		    			/usr/local/bin/beep -p 200 25
    					/usr/local/bin/beep -p 300 25 
    					/usr/local/bin/beep -p 400 25 
    					/usr/local/bin/beep -p 500 25 
    					/usr/local/bin/beep -p 600 25 
    					/usr/local/bin/beep -p 700 25 
    					/usr/local/bin/beep -p 800 25 
    					/usr/local/bin/beep -p 900 25 
    					/usr/local/bin/beep -p 1000 50
    				fi
    
    			fi
    
    	else
    		echo "PINGTEST: All pings failed twice." | logger
    		#echo "PINGTEST: All pings failed twice. Rebooting..." | logger
    		#/sbin/shutdown -r now | logger
    		exit 1
    	fi
    
    COUNT=`expr $COUNT + 1`
    done
    
    

Log in to reply