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

Bash脚本实现Captive Portal自动登录故障排查求助

Troubleshooting Your Captive Portal Auto-Login Script

Let's break down the issues you're facing and fix them step by step.

First: Why Your Cron-Jobbed Script Isn't Working

Cron runs in a stripped-down environment that doesn't inherit your user's full PATH or session context. Here are the quick fixes:

1. Use Absolute Paths for Commands

Cron doesn't know where ping or wget are located by default. Replace the commands with their absolute paths (you can find these with which ping and which wget):

#!/bin/bash
PING="/usr/bin/ping"
WGET="/usr/bin/wget"

if $PING -q -c 1 -W 1 8.8.8.8 >/dev/null 2>&1; then
    echo "The network is up"
else
    echo "The network is down"
    # Rest of your login logic here
fi

2. Add Logging to Debug Cron Issues

Redirect output to a log file so you can see exactly what's happening when cron runs the script:

# Add this at the top of your script
LOG_FILE="/var/log/captive-portal-login.log"

# Modify your echo statements to write to the log
echo "$(date): Network is up" >> $LOG_FILE
# And for the failure case:
echo "$(date): Network is down, attempting login" >> $LOG_FILE

3. Verify Cron Job Configuration

Make sure your cron job points to the full path of your script, and that the script has execute permissions:

  • Give the script execute rights: chmod +x /path/to/your/script.sh
  • Edit your crontab with crontab -e and add:
    * * * * * /path/to/your/script.sh >> /var/log/captive-portal-login.log 2>&1
    

Second: Fixing the Captive Portal Login Flow

Your current wget command targets the wrong URL, and captive portals with unique URL parameters require a multi-step flow to log in. Here's how to handle it:

The Problem with Unique URL Parameters

When you connect to the captive portal, it redirects you to a unique URL (like http://landing6.wifi.id/landing?abc123) to track your session. You can't skip this step—you need to first fetch this dynamic page, extract any required session tokens or parameters, then submit your login credentials.

Modified Script with Proper Login Flow

Here's an updated script that handles the dynamic parameters and session cookies:

#!/bin/bash
PING="/usr/bin/ping"
WGET="/usr/bin/wget"
LOG_FILE="/var/log/captive-portal-login.log"
USERNAME="your-portal-username"
PASSWORD="your-portal-password"

# Check network connectivity
if $PING -q -c 1 -W 1 8.8.8.8 >/dev/null 2>&1; then
    echo "$(date): Network is already up" >> $LOG_FILE
    exit 0
fi

echo "$(date): Network down, starting captive portal login" >> $LOG_FILE

# Step 1: Fetch the dynamic login page and save session cookies
$WGET --keep-session-cookies --save-cookies cookies.txt \
      --no-check-certificate --timeout 30 \
      http://landing6.wifi.id/ -O login-page.html 2>>$LOG_FILE

# Step 2: Extract required parameters from the login page (adjust based on actual HTML)
# Example: If the page has a hidden token field like <input name="token" value="xyz123">
TOKEN=$(grep -o 'name="token" value="[^"]*"' login-page.html | awk -F '"' '{print $4}')
# Add other parameters (like session ID) if the portal requires them

# Step 3: Submit the login form with credentials and extracted parameters
$WGET --load-cookies cookies.txt --keep-session-cookies --save-cookies cookies.txt \
      --post-data "username=$USERNAME&password=$PASSWORD&token=$TOKEN" \
      http://landing6.wifi.id/login -O login-result.html 2>>$LOG_FILE

# Clean up temporary files
rm -f cookies.txt login-page.html login-result.html

# Verify login success
if $PING -q -c 1 -W 1 8.8.8.8 >/dev/null 2>&1; then
    echo "$(date): Captive portal login successful" >> $LOG_FILE
else
    echo "$(date): Login failed—check credentials or portal parameters" >> $LOG_FILE
fi

Key Notes for This Script:

  • Cookies: Captive portals use cookies to track your session, so we save and reload them with --save-cookies and --load-cookies.
  • Dynamic Parameters: Use grep and awk to extract tokens from the login page. You'll need to inspect the portal's HTML (right-click the login page > "View Page Source") to find the exact parameter names.
  • User-Agent: Some portals block default wget user agents. Add --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" to your wget commands to mimic a browser.

内容的提问来源于stack exchange,提问作者Jimmy S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:53:07