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

LEDs and OpenVPN state established LED program short simple bash script

Scheduled Pinned Locked Moved OpenVPN
ledopenvpnstatescustomizevpn connection
1 Posts 1 Posters 350 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.
  • J
    JonathanLee
    last edited by JonathanLee Jan 4, 2024, 5:10 PM Jan 4, 2024, 9:06 AM

    Hello fellow Netgate community members,

    I wanted to share with you some cool code I have been working on for some time now. This code will check for specific states and adapt the LED's on an official Netgate appliance. Mine is a 2100.

    My VPN rule is 57 my guest wifi is rule 110

    Here is the code...

    #!/bin/sh
    check_current_states=$( pfctl -vvss | grep -e ', rule 110' -e ', rule 57' -e '192.168.1.11' -e '192.168.1.15' )
    res=1
    resb=1
    resc=1
    resd=1
    case "$check_current_states" in 
      *", rule 110"* ) res=0 ;;
    esac
    case "$check_current_states" in
      *192.168.1.11* ) resb=0 ;;
    esac
    case "$check_current_states" in
      *192.168.1.15* ) resc=0 ;;
    esac
    case "$check_current_states" in
      *", rule 57"* ) resd=0 ;;
    esac
    if [ $res = 0 ] && [ $resb = 0 ]; 
    then
      sysctl -q dev.gpio.2.led.1.pwm=1
      gpioctl -f /dev/gpioc2 3 duty 50 >/dev/null
      sysctl -q dev.gpio.2.led.2.pwm=1
      gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 6 duty 50 >/dev/null
    elif [ $res = 0 ];
    then
      sysctl -q dev.gpio.2.led.1.pwm=1
      gpioctl -f /dev/gpioc2 3 duty 0 >/dev/null
      sysctl -q dev.gpio.2.led.2.pwm=1
      gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 6 duty 50 >/dev/null
    elif [ $resb = 0 ];
    then
      sysctl -q dev.gpio.2.led.2.pwm=1
      gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 6 duty 0 >/dev/null
      sysctl -q dev.gpio.2.led.1.pwm=1
      gpioctl -f /dev/gpioc2 3 duty 50 >/dev/null
    else
      sysctl -q dev.gpio.2.led.1.pwm=1
      gpioctl -f /dev/gpioc2 3 duty 0 >/dev/null
      sysctl -q dev.gpio.2.led.2.pwm=1
      gpioctl -f /dev/gpioc2 6 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 7 duty 50 >/dev/null
      
    fi
    if [ $resc = 0 ] || [ $resd = 0 ];
    then
      sysctl -q dev.gpio.2.led.0.pwm=1
      gpioctl -f /dev/gpioc2 2 duty 50 >/dev/null
      gpioctl -f /dev/gpioc2 0 duty 50 >/dev/null
    else
      sysctl -q dev.gpio.2.led.0.pwm=1
      gpioctl -f /dev/gpioc2 2 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 0 duty 0 >/dev/null
    
    fi
    

    Overview:

    This area below is my variable that will store output from a pfctl and grep to any rule you want I have rule 110 and 57 as well as some private ip addresses.
    pfctl lists the current states on the firewall.

    What this code does for me is check for guest wifi use and if someone is on the guest network change first led to red, if my 192.168.1.11 is running enable and set the second LED to red and if 192.168.1.15 or a VPN is in online change the thrid LED to purple (meaning do not restart the firewall) if non of this occurs set first LED to green and turn off the others.

    check_current_states=$( pfctl -vvss | grep -e ', rule 110' -e ', rule 57' -e '192.168.1.11' -e '192.168.1.15' )
    

    This next section is my variables I use them as flags they are all set to one to instantiate them:

    res=1
    resb=1
    resc=1
    resd=1
    

    This next section checks for my conditions within the variable:

    case "$check_current_states" in 
      *", rule 110"* ) res=0 ;;
    esac
    
    case "$check_current_states" in
      *192.168.1.11* ) resb=0 ;;
    esac
    
    case "$check_current_states" in
      *192.168.1.15* ) resc=0 ;;
    esac
    
    case "$check_current_states" in
      *", rule 57"* ) resd=0 ;;
    esac
    

    This next section is where I have my if else rules that set the LEDS

    if [ $res = 0 ] && [ $resb = 0 ]; 
    then
      sysctl -q dev.gpio.2.led.1.pwm=1
      gpioctl -f /dev/gpioc2 3 duty 50 >/dev/null
      sysctl -q dev.gpio.2.led.2.pwm=1
      gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 6 duty 50 >/dev/null
    elif [ $res = 0 ];
    then
      sysctl -q dev.gpio.2.led.1.pwm=1
      gpioctl -f /dev/gpioc2 3 duty 0 >/dev/null
      sysctl -q dev.gpio.2.led.2.pwm=1
      gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 6 duty 50 >/dev/null
    elif [ $resb = 0 ];
    then
      sysctl -q dev.gpio.2.led.2.pwm=1
      gpioctl -f /dev/gpioc2 7 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 6 duty 0 >/dev/null
      sysctl -q dev.gpio.2.led.1.pwm=1
      gpioctl -f /dev/gpioc2 3 duty 50 >/dev/null
    else
      sysctl -q dev.gpio.2.led.1.pwm=1
      gpioctl -f /dev/gpioc2 3 duty 0 >/dev/null
      sysctl -q dev.gpio.2.led.2.pwm=1
      gpioctl -f /dev/gpioc2 6 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 7 duty 50 >/dev/null
      
    fi
    if [ $resc = 0 ] || [ $resd = 0 ];
    then
      sysctl -q dev.gpio.2.led.0.pwm=1
      gpioctl -f /dev/gpioc2 2 duty 50 >/dev/null
      gpioctl -f /dev/gpioc2 0 duty 50 >/dev/null
    else
      sysctl -q dev.gpio.2.led.0.pwm=1
      gpioctl -f /dev/gpioc2 2 duty 0 >/dev/null
      gpioctl -f /dev/gpioc2 0 duty 0 >/dev/null
    

    Screenshot 2024-01-04 at 12.51.21 AM.jpg

    Now save your code into your pfSense. chmod the bash script so it can execute this file...

    Screenshot 2024-01-04 at 12.56.33 AM.png

    Now set a cron job to run this every couple minutes or so.

    Screenshot 2024-01-04 at 12.57.58 AM.png

    For more help with how the colors on the LEDs work please reference this URL below. It is great I can't thank this guy enough. I hope he sees my code I made to work with the LED info he posted.

    https://www.zacharyschneider.ca/2019/12/customizing-leds-netgate-sg-3100/

    Bingo now you got custom LEDs that change when your states establish. You could set it to glow any color you want when OpenVPN connects.

    How do you get your state number.... Simple just click on the rule states area you made in your firewall and see what shows..

    Example:

    Screenshot 2024-01-04 at 1.03.49 AM.png

    Click here and it will show what rule id it is

    Screenshot 2024-01-04 at 1.02.54 AM.png
    Guest wifi for me is rule 105

    Make sure to upvote

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