#!/bin/sh # Configuration URL="http://grav.wsn.fi/allowed-mac" TEMP_FILE="/tmp/new_maclist.txt" SORTED_NEW="/tmp/new_maclist_sorted.txt" LOG_FILE="/tmp/logs.txt" WEB_LINK="/www/logs.txt" TARGET_SSID="Konepaja" # --- Setup: Ensure Web Accessibility --- # Check if the symbolic link exists and is a link (-L) if [ ! -L "$WEB_LINK" ]; then echo "Web link for logs missing. Creating link..." # Remove if it exists as a regular file to avoid errors, then link rm -f "$WEB_LINK" ln -s "$LOG_FILE" "$WEB_LINK" fi # Ensure the log file exists so the web server doesn't 404 [ ! -f "$LOG_FILE" ] && touch "$LOG_FILE" # --- Phase 1: Fetch and Sanitize --- echo "Fetching MAC list from $URL..." wget -qO- "$URL" | tr -d '\r' | tr 'a-z' 'A-Z' | grep -E '^([0-9A-F]{2}:){5}[0-9A-F]{2}' | sort | uniq > "$SORTED_NEW" if [ ! -s "$SORTED_NEW" ]; then echo "Error: Could not retrieve MAC list or list is empty." rm -f "$SORTED_NEW" exit 1 fi # --- Phase 2: Comparison and Update --- sections=$(uci show wireless | grep "=wifi-iface" | cut -d'.' -f2 | cut -d'=' -f1) any_change_detected=0 for section in $sections; do curr_ssid=$(uci -q get wireless."$section".ssid) if [ "$curr_ssid" = "$TARGET_SSID" ]; then # Get current list from UCI curr_macs=$(uci -q get wireless."$section".maclist | tr ' ' '\n' | tr 'a-z' 'A-Z' | sort | uniq) new_macs=$(cat "$SORTED_NEW") if [ "$curr_macs" = "$new_macs" ]; then echo "Interface $section is already up to date." continue fi echo "Change detected for $section. Updating..." any_change_detected=1 # Modify UCI settings uci set wireless."$section".macfilter='allow' uci -q delete wireless."$section".maclist while IFS= read -r mac; do uci add_list wireless."$section".maclist="$mac" done < "$SORTED_NEW" fi done # --- Phase 3: Commit and Log --- if [ "$any_change_detected" -eq 1 ]; then echo "Applying changes..." uci commit wireless /sbin/wifi reload # Append to RAM-based log echo "$(date '+%Y-%m-%d %H:%M:%S') - mac address list has been updated" >> "$LOG_FILE" else echo "No changes detected. Flash memory saved." fi # Cleanup rm -f "$SORTED_NEW"