You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash脚本检测Web页面有效性时出现误报问题的技术咨询

Troubleshooting False Positives in Your Web Page Validation Bash Script

Let's break down exactly what's causing those frustrating false positives in your script and walk through a fixed version with clear improvements.

Key Issues Behind the False Alerts

Your script has a few critical logic and reliability gaps that lead to incorrect results:

  • Inverted Exit Code Logic: Right now, you're checking if the SSH command fails (via ! ssh ...), not whether the problematic file size was detected. The SSH exit code mirrors the last remote command (grep), which returns non-zero if it doesn't find 12282. That means you're triggering the "Unable to..." message when the page is actually valid, and missing alerts when it's invalid.
  • Unreliable File Size Matching: Using ls -la index.html | grep -o 12282 can match the number 12282 in any part of the ls output—like the inode number, permissions, or timestamp—instead of only the actual file size.
  • No Wget Success Check: You don't verify if wget actually downloaded the page successfully. If the web server is down or returns an error, index.html might not exist, leading to ls errors and wrong grep results.
  • Background SSH Race Condition: The -f flag sends SSH to the background, which can cause incomplete output capture or your script proceeding before remote commands finish executing.

Fixed Script with Detailed Explanations

Here's a revised version that addresses all these issues:

BADSERVER=()
FILE="SERVER"  # Fixed variable assignment and added quotes
echo '' > "$FILE.txt"

# Replace with your actual host list loop (e.g., for device in $(cat hosts.txt); do)
for device in "${DEVICES[@]}"; do
    # Run remote commands with proper error handling
    ssh -n -i <sshkey> "$device" << 'EOF'
        # Safely clean up old files without unnecessary yes pipe
        rm -f index.*
        
        # Download page quietly and check if it succeeded
        if wget -q localhost:8080; then
            # Extract ONLY the file size field (5th column) from ls output
            file_size=$(ls -l index.html | awk '{print $5}')
            
            # Exact numeric comparison instead of grep matching
            if [ "$file_size" -eq 12282 ]; then
                echo "INVALID: Detected bad file size $file_size"
                exit 1  # Mark host as invalid
            else
                echo "VALID: File size $file_size is acceptable"
                exit 0
            fi
        else
            echo "ERROR: Failed to download page from localhost:8080"
            exit 2  # Mark download failure separately
        fi
EOF
    capture_exit=$?

    # Handle results based on remote exit codes
    case $capture_exit in
        0)
            echo "$device: Page validation passed" >> "$FILE.txt"
            ;;
        1)
            echo "$device: FAILED - Page has invalid size (12282)" >> "$FILE.txt"
            BADSERVER+=("$device")
            ;;
        2)
            echo "$device: ERROR - Could not download page" >> "$FILE.txt"
            ;;
        *)
            echo "$device: ERROR - SSH connection failed" >> "$FILE.txt"
            ;;
    esac
done

# Optional: Print summary of bad servers
if [ ${#BADSERVER[@]} -gt 0 ]; then
    echo -e "\nServers with invalid web pages:"
    printf '%s\n' "${BADSERVER[@]}"
fi

Critical Improvements Made

  1. Precise File Size Check: Swapped grep for awk to pull the exact 5th column (file size) from ls -l, then used numeric comparison (-eq) to avoid accidental matches on other fields.
  2. Wget Validation: Added a check for wget's exit code to ensure we only check the file size if the download was successful.
  3. Clear Exit Codes: Used distinct exit codes (0 for valid, 1 for invalid size, 2 for download failure) to differentiate between issues, eliminating ambiguous false positives.
  4. Removed Background SSH: Dropped the -f flag so the script waits for remote commands to finish before processing results, preventing race conditions.
  5. Safer Cleanup: Used rm -f instead of yes | rm to suppress errors when no old index.* files exist.
  6. Proper Quoting: Added quotes around variables to handle spaces or special characters in hostnames or file paths.

内容的提问来源于stack exchange,提问作者Danny Watson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:25:45