#!/bin/sh # Configuration BASE_URL="http://10.5.8.23/openwrt" CGI_LIST="cgi-bin.txt" SRV_LIST="srv.txt" RUN_SRV_LIST="run_repeatedly_srv.txt" RUN_CGI_LIST="run_repeatedly_cgi-bin.txt" LOG_FILE="/tmp/logs.txt" WEB_LINK="/www/logs.txt" SCRIPTS_HTML="/www/scripts.html" # --- Setup: Ensure Web Accessibility for Logs --- if [ ! -L "$WEB_LINK" ]; then rm -f "$WEB_LINK" ln -s "$LOG_FILE" "$WEB_LINK" fi [ ! -f "$LOG_FILE" ] && touch "$LOG_FILE" # Helper function to generate the HTML index of scripts generate_scripts_html() { TEMP_HTML="/tmp/scripts_nav.tmp" # Start building HTML in RAM echo "

Scripts available on this system, links below:

" > "$TEMP_HTML" # List executable .sh files in cgi-bin # Using 'ls' and filtering to ensure we only get filenames for f in /www/cgi-bin/*.sh; do if [ -x "$f" ]; then fname=$(basename "$f") echo "$fname
" >> "$TEMP_HTML" fi done # Add the Log link at the bottom echo "

they are logs

" >> "$TEMP_HTML" echo "View Logs" >> "$TEMP_HTML" # Comparison to avoid unnecessary disk wear if [ ! -f "$SCRIPTS_HTML" ] || ! cmp -s "$TEMP_HTML" "$SCRIPTS_HTML"; then echo "Updating $SCRIPTS_HTML index..." mv "$TEMP_HTML" "$SCRIPTS_HTML" # Log only if the index actually changed echo "$(date '+%Y-%m-%d %H:%M:%S') - Updated: $SCRIPTS_HTML (Index changed)" >> "$LOG_FILE" else echo "Scripts index is up to date. No write needed." rm -f "$TEMP_HTML" fi } # Helper function to process the sync lists process_list() { list_url="$1" target_dir="$2" list_tmp="/tmp/current_list.txt" echo "Fetching list from $list_url..." mkdir -p "$target_dir" wget -qO "$list_tmp" "$list_url" if [ $? -ne 0 ]; then echo "Error: Could not download list $list_url" return 1 fi tr -d '\r' < "$list_tmp" > "${list_tmp}.clean" mv "${list_tmp}.clean" "$list_tmp" while read -r filename; do filename=$(echo "$filename" | xargs) [ -z "$filename" ] && continue target_path="$target_dir/$filename" temp_path="/tmp/sync_item.tmp" echo "Checking $filename..." if wget -qO "$temp_path" "$BASE_URL/$filename"; then new_size=$(wc -c < "$temp_path") update_needed=0 if [ ! -f "$target_path" ]; then update_needed=1 reason="new file" else old_size=$(wc -c < "$target_path") if [ "$new_size" -ne "$old_size" ]; then update_needed=1 reason="size change ($old_size -> $new_size)" fi fi if [ "$update_needed" -eq 1 ]; then mv "$temp_path" "$target_path" echo "$(date '+%Y-%m-%d %H:%M:%S') - Updated: $target_path ($reason)" >> "$LOG_FILE" echo "Successfully updated $filename" else rm -f "$temp_path" fi chmod +x "$target_path" else echo "Error: Failed to download $filename" echo "$(date '+%Y-%m-%d %H:%M:%S') - Error: Failed to download $filename" >> "$LOG_FILE" fi done < "$list_tmp" rm -f "$list_tmp" } # Helper function to execute scripts run_scripts_from_list() { list_url="$1" target_dir="$2" exec_tmp="/tmp/exec_list.txt" wget -qO "$exec_tmp" "$list_url" [ $? -ne 0 ] && return 1 tr -d '\r' < "$exec_tmp" > "${exec_tmp}.clean" mv "${exec_tmp}.clean" "$exec_tmp" while read -r filename; do filename=$(echo "$filename" | xargs) [ -z "$filename" ] && continue full_path="$target_dir/$filename" if [ -x "$full_path" ]; then echo "Executing: $full_path" "$full_path" & fi done < "$exec_tmp" rm -f "$exec_tmp" } # --- Execution --- # 1. Sync Files process_list "$BASE_URL/$CGI_LIST" "/www/cgi-bin" process_list "$BASE_URL/$SRV_LIST" "/srv" # 2. Update the Web Index (HTML) - Only if cgi-bin contents changed generate_scripts_html # 3. Background execution run_scripts_from_list "$BASE_URL/$RUN_SRV_LIST" "/srv" run_scripts_from_list "$BASE_URL/$RUN_CGI_LIST" "/www/cgi-bin" echo "All tasks complete."