Project

General

Profile

Feature #12807 » PFSense_WAN2_Kill_Active_Sessions.txt

Bash script - Adam Di Vizio, 02/15/2022 10:52 PM

 
1
!/bin/sh
2
# check_backup_wan script
3
# ue0  is the 2nd WAN interface. igb0 is the primary WAN
4
# 1.1.1.1 is set as the monitor IP on the primary WAN interface
5
# The idea is to get the IP addresses of the primary and secondary WAN interfaces.
6
# If the primary WAN IP address is not available, assume the primary WAN is still down.
7
# Assuming the primary WAN is still up, check if there any live TCP connections on the backup WAN.
8
# If live TCP/UDP connections are found on the backup WAN, check that the primary WAN is responding to
9
# pings on the monitor IP address.  If the primary WAN is responding to pings, then kill the states
10
# on the backup WAN, and they will automatically reconnect over the primary WAN.
11

    
12
check_wan_time=`date "+%Y-%m-%d %H:%M:%S"`
13
check_wan='1.1.1.1'
14

    
15
wan_ipaddress=`ifconfig igb0 | grep 'inet ' | awk '{ print $2}' | cut -d'/' -f1`
16
wan2_ipaddress=`ifconfig ue0 | grep 'inet ' | awk '{ print $2}' | cut -d'/' -f1`
17
active_tcp_udp_sessions=$(pfctl -i ue0 -ss | grep 'tcp\|udp' | wc -l)
18

    
19
echo 'primary, backup WAN IP address ' ${wan_ipaddress} '(primary) ' ${wan2_ipaddress} '(backup)'
20
# check for valid primary WAN IP address.
21
if [ -z "${wan_ipaddress}" ]; then
22
  echo ${check_wan_time} '... primary WAN is still down (no WAN IP)' | tee -a /var/log/check_backup_wan.log
23
  exit 0
24
fi
25

    
26
# check for active connections on backup_wan
27
pfctl -i ue0 -ss | grep 'tcp\|udp'
28
wan2_liveconn=`pfctl -i ue0 -ss | grep 'tcp\|udp'`
29
if [ -n "${wan2_liveconn}" ]; then
30
# found active tcp/udp connection(s) on the backup wan interface
31
  ping -c 2 -t 2 -S ${wan_ipaddress} ${check_wan} > /dev/null 2>&1
32
  wan1_resp=$?
33
  wan_resp=`expr ${wan1_resp}`
34

    
35
  echo 'primary WAN ping check (0 means passed)' ${wan1_resp}
36

    
37
  if [ ${wan_resp} -eq 0 ]; then
38
    echo ${check_wan_time} 'Killing states and resetting connections on backup WAN. The total number of active TCP/UDP sessions on backup WAN are $active_tcp_udp_sessions and they have been disconnected as the primary WAN is now online.  The secondary WAN active sessions
39
are no longer required.' | tee -a /var/log/check_backup_wan.log
40
    echo -e "${check_wan_time} \n \n  The total number of active TCP/UDP sessions on backup WAN are $active_tcp_udp_sessions and they have been disconnected as the primary WAN is now online.  The secondary WAN active sessions are no longer required." | /usr/local/bin/mail.php -s"Cleared WAN2 Active Sessions"
41
    pfctl -k 192.168.1.118
42
  else
43
    echo ${check_wan_time} 'The primary WAN is still down (pings failing)' | tee -a /var/log/check_backup_wan.log
44
  fi
45
else
46
  echo ${check_wan_time} 'There are no active TCP or UDP connections found on backup WAN' | tee -a /var/log/check_backup_wan.log
47
fi
48

    
(1-1/2)