Skip to content

Instantly share code, notes, and snippets.

@martinheidegger
Last active January 17, 2026 04:05
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
DISABLED=0
PATTERN_WEEKDAY="0630 1900 1930 2130"
PATTERN_HOLIDAY="0630 1900 1930 2100"
read -r -d "" HOLIDAYS <<EOF
20260119
20260120
20260121
20260122
20260123
EOF
function now() {
date +"%H%M"
}
function today_date() {
date +"%a"
}
function today_weekday() {
date +"%Y%m%d"
}
function is_sat_sun() {
[[ "$1" = "Sat" || "$1" = "Sun" ]]
}
function is_holiday() {
[[ "${HOLIDAYS}" =~ "${1}" ]]
}
function to_min_of_day() {
time=$1
hour="${time:0:2}"
minute="${time:2:4}"
expr $hour \* 60 + $minute
}
function is_ok_time() {
if [[ "$DISABLED" == 1 ]]; then
return 1
fi
if is_sat_sun $2 || is_holiday $3; then
pattern=${PATTERN_HOLIDAY}
else
pattern=${PATTERN_WEEKDAY}
fi
ok=1
time=`to_min_of_day $1`
for flip in $pattern; do
flip_time=`to_min_of_day ${flip}`
if [[ $time -lt $flip_time ]]; then
break
fi
ok="$((( $ok ^ 1 )))"
done
return $ok
}
function is_unlocked() {
[[ "$(uci show firewall.@rule[0].enabled 2>/dev/null)" =~ "enabled='0'" ]]
}
function led() {
([[ "$2" = "on" ]] && echo "default-on" || echo "none") > /sys/class/leds/$1/trigger
}
function set_enabled() {
echo "rule enable=$1 ..."
uci set firewall.@rule[0].enabled=$1
uci set wireless.wifinet2.disabled=$1
uci commit
wifi reload
# ip link set dev wlan0 down
service firewall reload
echo "finished"
}
function lock() {
echo "locking..."
set_enabled 1
led "green:power" off
led "red:power" on
}
function unlock() {
echo "unlocking..."
set_enabled 0
led "red:power" off
led "green:power" on
}
case $1 in
enable)
unlock;;
unlock)
unlock;;
disable)
lock;;
lock)
lock;;
check)
if is_unlocked; then
echo "unlocked"
exit 1
else
echo "locked"
exit 0
fi
;;
*)
if ! is_unlocked; then
now="$(now)"
today_weekday="$(today_weekday)"
today_date="$(today_date)"
if is_ok_time $now $today_weekday $today_date; then
echo "in ok time ${now}/${today_weekday}/${today_date}"
unlock
else
echo "out of ok time ${now}/${today_weekday}/${today_date}"
fi
else
lock
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment