-
Notifications
You must be signed in to change notification settings - Fork 3
/
swcp-autologin.sh
72 lines (60 loc) · 1.85 KB
/
swcp-autologin.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
TARGET_ESSID="sapienza"
PORTAL_URL="https://wifi-cont1.uniroma1.it:8003/index.php?zone=wifi_sapienza"
PROBE_TIMEOUT=40
USER_AGENT="Mozilla/5.0 (X11; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0"
function log_debug {
echo $1
}
function is_internet_reachable {
local probe_timeout=$1
http_code=`curl --max-time $probe_timeout -s -o /dev/null -I -w "%{http_code}" http://clients3.google.com/generate_204`
if [ $http_code == 204 ]; then
return 1;
fi
return 0;
}
#control if user and password are being passed
if [ -z "$CPAL_USER" -o -z "$CPAL_PASS" ]; then
log_debug "'CPAL_USER' and 'CPAL_PASS' env variable must be set"
exit 1
fi
#controls if you are connected to the right AP
essid=`iwgetid -r`
if [ "$essid" != "$TARGET_ESSID" ]; then
log_debug "Not connected to the target AP"
exit 0;
fi
#controls if you are already logged
is_internet_reachable $PROBE_TIMEOUT
if [ $? == 1 ]; then
log_debug "Already logged in"
exit 0;
fi
# actual request
resp_page=$(
echo "auth_user=$CPAL_USER&auth_pass=$CPAL_PASS&accept=Continue" | \
curl -s --user-agent "$USER_AGENT" --retry 10 -d "@-" "$PORTAL_URL"
)
resp_out=$?;
# Check if curl exit with an error status code
if [ $resp_out -ne 0 ]; then
log_debug "Failed to sent login request: $resp_out"
exit 1;
fi
# If the response page contains the word 'logout' it means that
# we've successfully logged in
echo $resp_page | grep -iq "logout"
if [ $? -eq 0 ]; then
log_debug "Successfully logged in"
exit 0;
fi
# If the response page contains 'invalid credentials' it means that
# we had sended wrong username or password
echo $resp_page | grep -iq "Invalid credentials"
if [ $? -eq 0 ]; then
log_debug "Invalid credentials"
exit 3;
fi
log_debug "The login request has been sent but the response doesn't contain any meaningful data"
exit 1