Skip to content

Instantly share code, notes, and snippets.

@ALiangLiang
Last active August 21, 2017 07:09
Show Gist options
  • Save ALiangLiang/054b1c50cef4e82b94df98d7966fb4a9 to your computer and use it in GitHub Desktop.
Save ALiangLiang/054b1c50cef4e82b94df98d7966fb4a9 to your computer and use it in GitHub Desktop.
NCTU dorm network login script
#!/bin/sh
# This is a script to login network of NCTU dorm.
# You can place this script into startup folder.
# And then will auto login when your PC startup.
# Please fill in your account and password.
ACCOUNT=""
PASSWORD=""
# Get login page url.
URI=$(curl -i 'http://www.nctu.edu.tw' | grep "Location:" | awk '{print $2}')
# Grep magic code.
MAGIC=$(echo "$URI" | cut -d "?" -f 2)
# Build request headers.
ORIGIN=$(echo "$URI" | cut -d "/" -f -3)
DOMAIN=$(echo "$ORIGIN")/
HOST=$(echo "$DOMAIN" | cut -d "/" -f 3)
# Simulate login operation.
curl "$URI"
curl -v -i -X POST \
-H "Origin: $ORIGIN" \
-H "Host: $HOST" \
-H "Referer: $URI" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" \
-d "magic=$MAGIC&username=$ACCOUNT&password=$PASSWORD&4Tredir=http%3A%2F%2Fwww.nctu.edu.tw%2F" "$DOMAIN" \
&& echo "Login success." || echo "Login failed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment