Skip to content

Instantly share code, notes, and snippets.

@martinheidegger
Last active September 9, 2025 11:28
Show Gist options
  • Select an option

  • Save martinheidegger/1a4388cc490c48eb44f2cbc2900f4329 to your computer and use it in GitHub Desktop.

Select an option

Save martinheidegger/1a4388cc490c48eb44f2cbc2900f4329 to your computer and use it in GitHub Desktop.
OpenWRT - Wifi rule on button press
#!/bin/sh
MIN_TIME=0630 # Time of the day from when the wifi should be enabled
MAX_TIME=2215 # Time of the day until when the wifi will stay enabled
function is_disabled() {
[[ "$(uci show firewall.@rule[0].enabled 2>/dev/null)" =~ "enabled='0'" ]]
}
function led_on() {
echo "default-on" > /sys/class/leds/$1/trigger
}
function now() {
date +"%H%M"
}
function to_min_of_day() {
time=$1
hour="${time:0:2}"
minute="${time:2:4}"
expr $hour \* 60 + $minute
}
function is_ok_time() {
time=`to_min_of_day $1`
start=`to_min_of_day $MIN_TIME`
end=`to_min_of_day $MAX_TIME`
[[ $time -ge $start && $time -lt $end ]]
}
function led_off() {
echo "none" > /sys/class/leds/$1/trigger
}
function set_enabled() {
echo "enable=$1 ..."
uci set firewall.@rule[0].enabled=$1
uci commit
service firewall reload
echo "finished"
}
function enable() {
set_enabled 1
led_off "green:power"
led_on "red:power"
}
function disable() {
echo "disabling..."
set_enabled 0
led_off "red:power"
led_on "green:power"
}
case $1 in
enable)
enable;;
disable)
disable;;
check)
if is_disabled; then
echo "disabled"
case $1 in
enable)
enable;;
disable)
disable;;
check)
if is_disabled; then
echo "disabled"
exit 1
else
echo "enabled"
exit 0
fi
;;
*)
if is_disabled; then
if is_ok_time `now`; then
enable
else
echo "out of ok time"
fi
else
disable
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment