-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsona-checker.sh
126 lines (92 loc) · 4.35 KB
/
sona-checker.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
#Quick 'N Dirty checker for sona
#To be scheduled by crontab
#_______________________________________________________________________
# USER OPTIONS
#Set a value here for debugs. Empty = no messages
DEBUG=""
#Set a value here to enable random interval sleeps,
#to make it less obvious we're botting, or w.e.
SLEEPER="ON"
#These files must be user defined
#1: Emails file; 1 email address per line. Sends emails to these.
#2: Credentials file; line 1 is username, line 2 is password
EMAILS_FILE="email_list.txt"
CREDENTIALS_FILE="credentials.txt"
#Your page to check; modify to your sona instance
#Can technically only use the one page but...
DEFAULT_PAGE="https://wlu-ls.sona-systems.com/default.aspx"
STUDIES_PAGE="https://wlu-ls.sona-systems.com/all_exp_participant.aspx"
#These files will be generated to keep track
ID_FILE="ids.txt"
COOKIE_FILE="cookies.txt"
#_______________________________________________________________________
login() {
#Default session id
SESSIONID='251l33svkwftvv34ytpsl5p1'
#Grab credentials
USERNAME=$(sed '1q;d' $CREDENTIALS_FILE)
PASSWORD=$(sed '2q;d' $CREDENTIALS_FILE) #Need to be stored with html url encoding
#Login request; returns required cookie into cookies.txt
DATA="__LASTFOCUS=&__VIEWSTATE=%2FwEPDwUJNjM2MDQ1NTgyZGSHi2XgX%2FUE1%2F9%2BPCRY6NJ4nXYVWCVXfAYIFvqAveLC3A%3D%3D&__VIEWSTATEGENERATOR=CA0B0334&__EVENTTARGET=&__EVENTARGUMENT=&__EVENTVALIDATION=%2FwEdAAVJNJYm0f4uWg7cS6joTPw5UIlPJ3shF6ZfHx5cHAdswX1Gsa4Qp9IFMNZyT1m%2FORlOGPoKvJSxXl507%2BPWyULdk0IaRa81gSyF%2Ft2E7n3iJWU%2BD9YgP8jtn3s5kkIRi4NZc9SrPpif7I8VynwW%2BcCE&ctl00%24ContentPlaceHolder1%24return_experiment_id=&ctl00%24ContentPlaceHolder1%24userid=${USERNAME}&ctl00%24ContentPlaceHolder1%24pw=${PASSWORD}&ctl00%24ContentPlaceHolder1%24default_auth_button=Log+In"
COOKIE="Cookie: language_pref=EN; ASP.NET_SessionId=$SESSIONID; cookie_ck=Y;"
#Login request; returns required cookie into cookies.txt
curl --cookie-jar $COOKIE_FILE -s "https://wlu-ls.sona-systems.com/default.aspx" -H "$COOKIE" -d "$DATA" > /dev/null
# Gross way to grab cookie in a hard-coded method
WEBHOME=$(sed '5q;d' "$COOKIE_FILE" | awk '{print $7}')
echo "Cookie: language_pref=EN; ASP.NET_SessionId=$SESSIONID; cookie_ck=Y; WEBHOME=$WEBHOME"
}
#To avoid potential of getting banned, have random sleep to avoid bot flagging
if [ ! -z "$SLEEPER" ]; then
RAND=$(shuf -i 1-600 -n 1)
sleep "${RAND}s"
fi
#Login and save the cookie
COOKIE="$(login)"
#Check the studies page
curl -s "$STUDIES_PAGE" -H "$COOKIE" > studies.html
#Check if loading page successful; if not, send alerts
CHECK1="$(grep "Object moved to" studies.html)"
CHECK2="$(grep "Bad Request" studies.html)"
if [ ! -z "$CHECK1" ] || [ ! -z "$CHECK2" ]; then
if [ ! -z "$DEBUG" ]; then echo "Failed to login properly, cannot load page."; fi
cat "email_list.txt" | while read -r address; do
if [ -z "$address" ]; then continue; fi
SUBJECT="Failed to check studies page. Cookie error?"
TEXT="$(cat $COOKIE_FILE)"
echo -e "$TEXT" | mail -s "$SUBJECT" "$address"
done
exit
fi
#Use a file as a flag for if an email is sent.
#File instead of a variable because of context issues
printf "" > email_ids.txt
grep 'experiment_id=' studies.html | while read -r id ; do
#Skip over duplicate div
if echo "$id" | grep -q "btn"; then continue; fi
if [ -z "$id" ]; then continue; fi
# sed -e '1,/TERMINATE/d' studies.html #refernce for later additions
ID_NUM=$(echo "$id" | sed 's/.*experiment_id=//' | sed 's/">.*//')
if grep -q "$ID_NUM" "$ID_FILE"; then
#Already encountered.
if [ ! -z "$DEBUG" ]; then echo "ID $ID_NUM already exists"; fi
else
#Store variable in file
echo "$ID_NUM" >> "$ID_FILE";
if [ ! -z "$DEBUG" ]; then echo "Sending out alert emails for $ID_NUM..."; fi
printf "$ID_NUM " >> email_ids.txt
fi
done
EMAIL_IDS=$(cat email_ids.txt)
if [ ! -z "$EMAIL_IDS" ]; then
cat "email_list.txt" | while read -r address; do
if [ -z "$address" ]; then continue; fi
SUBJECT="New Study(s) Available on Sona | ID(s):$EMAIL_IDS"
TEXT="$STUDIES_PAGE"
# SENDER='SonaCheckerBot <[email protected]>'
echo -e "$TEXT" | mail -s "$SUBJECT" "$address"
done
fi
#Remove temp html page
# rm studies.html
rm email_ids.txt