• Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Search
  • Register
  • Login
Netgate Discussion Forum
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Search
  • Register
  • Login

Looking for someone to write a script

Scheduled Pinned Locked Moved Community Job Board
10 Posts 3 Posters 1.7k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S
    sef1414
    last edited by May 30, 2023, 7:57 PM

    I’m looking for assistance getting an unsupported version of high availability running.

    I currently have Pfsense HA / CARP working, on local interfaces only. This is because I have dynamic IPs and cannot get static ones. On the WAN side of things, I simply have WAN gateways disabled on my secondary interface. I’m not so much concerned about seamless failover, as I am avoiding long periods of down time.

    Currently, if my primary pfsense box failed, I could enable WAN gateways on secondary box, and should be back up in a matter of minutes. However, that only works if I’m sitting in front of the computer.
    I’d like to have this automated, so I’m looking for a script that will monitor for HA / CARP status, and enable WAN gateways when a box becomes master, and disable WAN gateways when a box becomes the secondary.
    I’m happy to pay for help with this. I’m thinking around $300, I imagine someone well versed with pfsense could accomplish this in short order. Would consider more if its warranted.

    D W 2 Replies Last reply May 30, 2023, 8:14 PM Reply Quote 0
    • D
      Dobby_ @sef1414
      last edited by May 30, 2023, 8:14 PM

      @sef1414 said in Looking for someone to write a script:

      I’m not so much concerned about seamless failover, as I am avoiding long periods of down time.

      High Availability
      High Availability Configuration Example

      It should be able to archive that the second one is
      "jumping" in if the first one fails.

      #~. @Dobby

      Turris Omnia - 4 Ports - 2 GB RAM / TurrisOS 7 Release (Btrfs)
      PC Engines APU4D4 - 4 Ports - 4 GB RAM / pfSense CE 2.7.2 Release (ZFS)
      PC Engines APU6B4 - 4 Ports - 4 GB RAM / pfSense+ (Plus) 24.03_1 Release (ZFS)

      1 Reply Last reply Reply Quote 0
      • W
        w0w @sef1414
        last edited by May 31, 2023, 1:29 AM

        @sef1414
        What exactly your WAN is? Is it PPPoE?

        S 1 Reply Last reply May 31, 2023, 11:15 PM Reply Quote 0
        • S
          sef1414 @w0w
          last edited by May 31, 2023, 11:15 PM

          @w0w

          I have two DHCP WAN connections and one PPPoE.

          W 1 Reply Last reply Jun 1, 2023, 3:52 AM Reply Quote 0
          • W
            w0w @sef1414
            last edited by Jun 1, 2023, 3:52 AM

            @sef1414
            So it is multi wan? Each box has one DHCP and PPPoE also is configured on both? Why DHCP WAN should be disabled when the box is not primary? Does this violate the provider's rules? Provide more information of what is wrong and what you want to do. I am already using one script that automatically puts PPPoE down or up, monitoring the status of the firewall, but every configuration is different and in your case it may not work the way you want.

            S 1 Reply Last reply Jun 1, 2023, 1:41 PM Reply Quote 1
            • S
              sef1414 @w0w
              last edited by Jun 1, 2023, 1:41 PM

              @w0w

              I have multi WAN for load balancing / WAN failover scenarios (separate from high availability needs). I can't have the same active WAN on both primary and secondary pfsense, as it will create issues (duplicate MACs, VPN problems, etc.)

              W 1 Reply Last reply Jun 1, 2023, 2:47 PM Reply Quote 0
              • W
                w0w @sef1414
                last edited by Jun 1, 2023, 2:47 PM

                @sef1414
                So only DHCP WAN need to be supported by script?

                W 1 Reply Last reply Jun 1, 2023, 3:44 PM Reply Quote 0
                • W
                  w0w @w0w
                  last edited by w0w Jun 2, 2023, 4:47 AM Jun 1, 2023, 3:44 PM

                  @sef1414

                  #!/bin/sh
                  
                  LOCKFILE="/var/run/run.sh.lock"
                  
                  # Check if the lock file exists and exit if it does
                  if [ -f "${LOCKFILE}" ]; then
                      # Check if the process that created the lock file is still running
                      LOCKPID=$(cat "${LOCKFILE}")
                      if [ -n "$(ps -p "${LOCKPID}" -o pid=)" ]; then
                          echo "Script is already running with PID ${LOCKPID}. Exiting."
                          exit 1
                      else
                          # Remove stale lock file
                          rm -f "${LOCKFILE}"
                      fi
                  fi
                  
                  # Create lock file with current PID
                  echo "$$" > "${LOCKFILE}"
                  
                  #############################################################################
                  # Interfaces configuration
                  # Put your LAN card ifconfig name here, e.g., "igc1"
                  
                  LAN="igc1"
                  
                  # Put your LAN CARP VIP VHID number, e.g., "vhid 5"
                  
                  VIP_VHID_IPv4_LAN="vhid 5"
                  
                  # Put your DHCP WAN ifconfig name here
                  
                  WAN_DHCP="igc0"
                  
                  # Put your WANDHCP interface "down" status
                  # Issue "ifconfig igc0 down" command without quotes
                  # Wait a bit and issue "ifconfig igc0" command
                  # Sample answer
                  # igc0: flags=8c22<BROADCAST,OACTIVE,SIMPLEX,MULTICAST> metric 0 mtu 1500
                  # So use 8c22
                  
                  WANDHCP_DWN="8c22"
                  
                  #############################################################################
                  case "$1" in
                  start)
                      logger "Monitor CARP status"
                  
                      # Looping
                      INPUT_STRING=hello
                      while [ "$INPUT_STRING" != "bye" ]; do
                          sleep 30
                          CHECKCARPSTATUS=$(ifconfig $LAN | grep -o "MASTER $VIP_VHID_IPv4_LAN" | head -n 1)
                          if [ "$CHECKCARPSTATUS" = "MASTER $VIP_VHID_IPv4_LAN" ]; then
                              check_WAN_DHCP_if=$(ifconfig $WAN_DHCP | grep -o 'UP')
                              if [ "$check_WAN_DHCP_if" = 'UP' ]; then
                                  echo "WANDHCP already up"
                              else
                                  ifconfig $WAN_DHCP up
                                  logger "STATUS: MASTER, WANDHCP UP"
                              fi
                          else
                              check_WAN_DHCP_if=$(ifconfig $WAN_DHCP | grep -o "$WANDHCP_DWN")
                              if [ "$check_WAN_DHCP_if" = "$WANDHCP_DWN" ]; then
                                  echo "WANDHCP already down"
                              else
                                  ifconfig $WAN_DHCP down
                                  logger "STATUS: BACKUP. WANDHCP DOWN"
                              fi
                          fi
                      done
                      echo "end"
                      ;;
                  stop)
                      exit 0
                      ;;
                  restart)
                      exit 0
                      ;;
                  esac
                  
                  exit 0
                  
                  

                  Theoretically, this one adjusted should be run on every node in CARP. The main idea is just to put down WAN DHCP interface when firewall is not MASTER, script checks the firewall status every 30 seconds and makes decisions what to do with WAN. The script also checks for instances already running and also using case just for compatibility with service like mode.
                  Use at your own risk.

                  S 1 Reply Last reply Jun 2, 2023, 12:24 AM Reply Quote 0
                  • S
                    sef1414 @w0w
                    last edited by Jun 2, 2023, 12:24 AM

                    @w0w

                    Cool thanks, will give this a shot this weekend. What directory do you store this in and how do you ensure it starts running when pfsense starts up?

                    W 1 Reply Last reply Jun 2, 2023, 4:43 AM Reply Quote 1
                    • W
                      w0w @sef1414
                      last edited by Jun 2, 2023, 4:43 AM

                      @sef1414
                      Name it "run.sh", copy to pf and chmod according documentation
                      https://docs.netgate.com/pfsense/en/latest/development/boot-commands.html#shell-script-option
                      You will see messages in the system log like those quoted in the script after logger command.

                      1 Reply Last reply Reply Quote 0
                      10 out of 10
                      • First post
                        10/10
                        Last post
                      Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.
                        This community forum collects and processes your personal information.
                        consent.not_received