Skip to content

Instantly share code, notes, and snippets.

@winwinashwin
Last active August 2, 2022 12:44
Show Gist options
  • Save winwinashwin/7844d6801b0da7b8d42c60c5c5525679 to your computer and use it in GitHub Desktop.
Save winwinashwin/7844d6801b0da7b8d42c60c5c5525679 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Temporary cookie jar
tmpfile=$(mktemp)
cleanup() {
rm -rf "$tmpfile"
}
trap cleanup EXIT
trap cleanup SIGINT
usage() {
cat <<EOM
NAME
netaccess.sh - Authorize machine in IITM Network
SYNOPSIS
$0 -u LDAP_USERNAME -p LDAP_PASSWORD [-Ph]
OPTIONS
-u
LDAP username
-p
LDAP password
-P
Enable IITM remote proxy (intended for development outside IITM network)
-h
Show help
AUTHOR
Ashwin A Nayar <ashwin5059198@gmail.com>
EOM
}
# Default values goes here
LDAP_USR=""
LDAP_PWD=""
ENABLE_PROXY=false
while getopts ":u:p:Ph" options; do
case "$options" in
u)
LDAP_USR=${OPTARG}
;;
p)
LDAP_PWD=${OPTARG}
;;
h)
usage
exit 0
;;
P)
ENABLE_PROXY=true
;;
:)
echo "Error: -${OPTARG} requires an argument."
usage
exit 1
;;
*)
usage
exit 1
;;
esac
done
[[ -z "$LDAP_USR" || -z "$LDAP_PWD" ]] && { usage; exit 1; }
echo "++ INFO ++ LDAP Username: $LDAP_USR"
if [[ "$ENABLE_PROXY" = true ]]; then
echo "!! WARN !! IITM remote proxy is enabled"
export HTTPS_PROXY="https://${LDAP_USR}:${LDAP_PWD}@remote.iitm.ac.in:8372"
fi
# Login
post_login_url=$(curl \
-kLs \
-o /dev/null \
--ciphers ':HIGH:!DH:!aNULL' \
-c "$tmpfile" \
-X POST \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d "userLogin=${LDAP_USR}&userPassword=${LDAP_PWD}&submit=" \
-w %{url_effective} \
https://netaccess.iitm.ac.in/account/login
)
# Verify login
if [[ "$post_login_url" == "https://netaccess.iitm.ac.in/account/index" ]]; then
echo "++ INFO ++ Logged in"
else
echo "-- FAIL -- Login failed, try connecting manually"
exit 1
fi
# Approve machine
curl \
-kLs \
-o /dev/null \
--ciphers ':HIGH:!DH:!aNULL' \
-b "$tmpfile" \
-X POST \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d "duration=2&approveBtn=" \
https://netaccess.iitm.ac.in/account/approve \
&& \
echo "++ INFO ++ Request sent to approve machine" \
|| \
{ echo "-- FAIL -- Error sending request to approve machine" ; exit 1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment