#!/usr/bin/env ash phone_number="" password="" interface="" echo "Waiting for IP address on interface ${interface}..." >&2 max_attempts=30 attempt=0 while [ -z "$ip" ] && [ $attempt -lt $max_attempts ]; do ip=$(ip addr | awk -v iface_name="$interface" '/^[0-9]+:/{iface=$2; sub(/:$/,"",iface)} iface==iface_name && /inet /{split($2,a,"/"); print a[1]; exit}') if [ -z "$ip" ]; then sleep 1 attempt=$((attempt+1)) echo "Attempt ${attempt}: Still no IP. Retrying..." >&2 fi done if [ -z "$ip" ]; then echo "Error: Could not get IP address for interface ${interface} after ${max_attempts} attempts. Exiting." >&2 exit 1 fi mac=$(ip addr | awk -v iface_name="$interface" '/^[0-9]+:/{iface=$2; sub(/:$/,"",iface)} iface==iface_name && /link\/ether/{print $2; exit}') echo "ip = ${ip}" echo "mac = ${mac}" curl_command="curl -X POST \"http://192.1.1.55:801/eportal/?c=ACSetting&a=Login&protocol=http:&hostname=192.1.1.55&iTermType=1&wlanuserip=${ip}&wlanacip=null&wlanacname=null&mac=${mac}&ip=${ip}&enAdvert=0&queryACIP=0&jsVersion=2.4.3&loginMethod=1\" -H \"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36\" -H \"Content-Type: application/x-www-form-urlencoded\" -H \"Referer: http://192.1.1.55/\" --data-raw \"DDDDD=%2C0%2C${phone_number}%40unicom&upass=${password}&R1=0&R2=0&R3=0&R6=0¶=00&0MKKey=123456&buttonClicked=&redirect_url=&err_flag=&username=&password=&user=&cmd=&Login=&v6ip=\" -v" curl_raw_output=$(mktemp) location_query_string_tmp=$(mktemp) parsed_error_params_tmp=$(mktemp) trap "rm -f \"$curl_raw_output\" \"$location_query_string_tmp\" \"$parsed_error_params_tmp\"" EXIT eval "$curl_command" 2>&1 | \ tee "$curl_raw_output" | \ grep -E '^< Location:' > "$location_query_string_tmp" location_query=$(cat "$location_query_string_tmp") if echo "$location_query" | grep -q "3.htm"; then echo "Authentication successful." exit 0 else echo "$location_query" | \ sed -E 's/.*ACLogOut=([^&]*).*RetCode=([^&]*).*ErrorMsg=([^&]*).*/ACLogOut: \1\nRetCode: \2\nErrorMsg: \3/' > "$parsed_error_params_tmp" parsed_params_content=$(cat "$parsed_error_params_tmp") if [ -n "$parsed_params_content" ]; then echo "Authentication failed:" echo "${parsed_params_content}" exit 1 elif [ -n "$location_query" ]; then echo "Authentication failed due to unknown reason. Location header query: ${location_query}" exit 1 else echo "Authentication failed: No relevant Location header or query string found to confirm success, and no explicit error messages were detected." exit 1 fi fi