#!/bin/sh
USERNAME=username
PASSWORD=password
# Set this to the entry you choose from the "Authentication" options
PROVIDER="Guest Users"
# Set this to the URL of the captive portal
PORTAL=cas.example.com

IP=`ip addr show $IFACE | grep -o '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*' | head -n 1`

# Set this to the IP range of the unauthenticated subnet
CHECK=${IP#10.128.11.}
if [ ${#CHECK} -lt 4 ]; then
	URL=`wget --no-check-certificate -q http://$PORTAL -O - | awk -F\' '{print $4}' | sed 's/1;URL=//g'`
	wget --no-check-certificate -q -O /tmp/page1 "$URL"
	IP=`awk -F\" '{ print $2 }' < /tmp/page1 | grep '[0-9]\.' | head -n 1`
	SESSION=`awk -F\" '{ print $2 }' < /tmp/page1 | grep '^[-0-9][-0-9]*$' | tail -n 1`
	CM=`echo "$URL" | sed 's/^.*cm=//g' | sed 's/&.*//g'`
	rm /tmp/page1
	wget --no-check-certificate -q -O /dev/null --post-data='reqFrom=perfigo_login.jsp&uri=http://$PORTAL/&cm='$CM'&userip='$IP'&session='$SESSION'&pm=&index=0&pageid=-1&compact=false&registerGuest=NO&userNameLabel=Username&passwordLabel=Password&guestUserNameLabel=Guest ID (eMail address)&guestPasswordLabel=Password&username='$USERNAME'&password='$PASSWORD'&provider='"$PROVIDER" "https://$PORTAL/auth/perfigo_cm_validate.jsp"
fi

