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 12282can match the number 12282 in any part of thelsoutput—like the inode number, permissions, or timestamp—instead of only the actual file size. - No Wget Success Check: You don't verify if
wgetactually downloaded the page successfully. If the web server is down or returns an error,index.htmlmight not exist, leading tolserrors and wronggrepresults. - Background SSH Race Condition: The
-fflag 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
- Precise File Size Check: Swapped
grepforawkto pull the exact 5th column (file size) fromls -l, then used numeric comparison (-eq) to avoid accidental matches on other fields. - Wget Validation: Added a check for
wget's exit code to ensure we only check the file size if the download was successful. - 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.
- Removed Background SSH: Dropped the
-fflag so the script waits for remote commands to finish before processing results, preventing race conditions. - Safer Cleanup: Used
rm -finstead ofyes | rmto suppress errors when no oldindex.*files exist. - Proper Quoting: Added quotes around variables to handle spaces or special characters in hostnames or file paths.
内容的提问来源于stack exchange,提问作者Danny Watson
相关产品推荐
相关产品推荐

