Auto DHCP Renew not working on WAN (How to fix it), updated script



  • First of all thank you @BennTech for this original script and thank you @dice81 for updating it.

    This solved my problems when WAN connection goes down if ISP changes something at their end. Updated it a little more. I want to know when connection has had a hick-up so I know why example VPN connections go down. Notification email is being sent using pfSense notification settings (does not leave if it gets rebooted. Then you get bootup complete notification from pfSense itself). Date and time is written to logfile with finnish date and time format.

    You can find the original script here http://forum.pfsense.org/index.php/topic,17243.0.html

    For those of you who are nix handicapped like me, installing and activating this script is easy to do via the web interface.
    Go to Diagnostics > Edit file
    Cut & paste the code below, editing the user variables to match your settings.
    Save the file as /usr/local/bin/pingtest.sh (or path/name of your choosing)
    Go to Diagnostics > Command
    Execute the command "chmod +x /usr/local/bin/pingtest.sh" (makes the file executable)
    Go to System > Packages and install the Cron package
    Go to Services > Cron
    Install a new cron with settings "/5 * * * * root /usr/local/bin/pingtest.sh" (runs test every 5 minutes)

    If you want to verify the test is running, uncomment (remove the "#") in front the "echo" commands on lines 42, 46 & 47. Then, use Diagnostics > Edit file to view the contents of /root/pingtest.log. There should be a log entry every 5 minutes. Once you're sure it's running, be sure to add the comments back so you don't fill your hard drive with this script's status message.

    #!/bin/sh
    
    #=====================================================================
    # pingtest.sh, v1.0.3
    # Created 2009 by Bennett Lee
    # 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. Also email is being sent using pfSense notification settings.
    # (3) If all pings fail again after reset, then reboots pfSense.
    #
    # History
    # 1.0.3   Modified date format and added emailing using pfSense notification settings (zazuu0)
    # 1.0.2   Added turn dhclient on for the interface. (Dice81)
    # 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 yahoo.com 24.93.40.36 24.93.40.37"
    # Interface to reset, usually your WAN
    BOUNCE=hn0
    # Log file
    LOGFILE=/root/pingtest.log
    # Get hostname
    HOSTNAME=$(hostname)
    #=====================================================================
    
    COUNT=1
    while [ $COUNT -le 2 ]
    do
    
    	for DEST in $ALLDEST
    	do
    		#echo `date "+%d.%m.%Y %H:%M:%S"` "Pinging $DEST" >> $LOGFILE
    		ping -c1 $DEST >/dev/null 2>/dev/null
    		if [ $? -eq 0 ]
    		then
    			#echo `date "+%d.%m.%Y %H:%M:%S"` "Ping $DEST OK." >> $LOGFILE
    			#echo "Pingtest success on interface $BOUNCE on host $HOSTNAME." | /usr/local/bin/mail.php -s"$HOSTNAME pingtest OK"
    			exit 0
    		fi
    	done
    
    	if [ $COUNT -le 1 ]
    	then
    		echo `date "+%d.%m.%Y %H:%M:%S"` "All pings failed. Resetting interface $BOUNCE." >> $LOGFILE
    		/sbin/ifconfig $BOUNCE down
    		# Give interface time to reset before bringing back up
    		sleep 10
    		/sbin/ifconfig $BOUNCE up
    		# Give WAN time to establish connection
    		sleep 20
    		dhclient $BOUNCE
    		sleep 20
    		echo "WAN connection detected being down on $HOSTNAME. Interface $BOUNCE reset is done." | /usr/local/bin/mail.php -s"$HOSTNAME interface $BOUNCE reset"
    		echo `date "+%d.%m.%Y %H:%M:%S"` 'email "reset" was created' >> $LOGFILE
    	else
    		echo `date "+%d.%m.%Y %H:%M:%S"` "All pings failed twice. Rebooting..." >> $LOGFILE
    		/sbin/shutdown -r now >> $LOGFILE
    		exit 1
    	fi
    
    	COUNT=`expr $COUNT + 1`
    done