-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_devices.sh
120 lines (100 loc) · 2.84 KB
/
check_devices.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
#!/bin/sh
# this script is being called every 10 mins to check if the current connected devices
# have consumed quota more than it should have.
# and disconnects them if they reached the maximum amount of quota
parse_json() {
for param in gatewayname gatewayaddress gatewayfqdn version client_type mac ip clientif session_start session_end \
last_active token state custom download_rate_limit_threshold download_packet_rate download_bucket_size \
upload_rate_limit_threshold upload_packet_rate upload_bucket_size \
download_quota upload_quota download_this_session download_session_avg upload_this_session upload_session_avg
do
val=$(echo "$param_str" | grep "$param" | awk -F'"' '{printf "%s", $4}')
if [ "$val" = "null" ]; then
val="Unlimited"
fi
if [ -z "$val" ]; then
eval $param=$(echo "Unavailable")
else
eval $param=$(echo "\"$val\"")
fi
done
}
do_ndsctl () {
local timeout=4
for tic in $(seq $timeout); do
ndsstatus="ready"
ndsctlout=$(ndsctl $ndsctlcmd)
for keyword in $ndsctlout; do
if [ $keyword = "locked" ]; then
ndsstatus="busy"
sleep 1
break
fi
if [ $keyword = "Failed" ]; then
ndsstatus="failed"
break
fi
if [ $keyword = "authenticated." ]; then
ndsstatus="authenticated"
break
fi
done
keyword=""
if [ $tic = $timeout ] ; then
busy_page
fi
if [ "$ndsstatus" = "authenticated" ]; then
break
fi
if [ "$ndsstatus" = "failed" ]; then
break
fi
if [ "$ndsstatus" = "ready" ]; then
break
fi
done
}
#getting all devices that are connected to the account being searched
check_account() {
logfile="/tmp/ndslog/binauthlog.log"
userstr="username=$username"
total=0
echo "$userstr"
tokens=$(cat $logfile | awk -F "$userstr" 'NF>1{print $0}'| awk -F"token=" '{print $2}' | awk -F", " '{print $1}' | sort |uniq)
for token in $tokens; do
ndsctlcmd="json $token"
do_ndsctl
param_str=$ndsctlout
if [ "$param_str" = "{}" ] || [ "$ndsctlstatus" = "busy" ]; then
continue
fi
parse_json
if [ "$state" = "Authenticated" ]; then
up=$(($upload_this_session/1024))
down$(($download_this_session)/1024))
total=$(($total+($upload_this_session+$download_this_session)/1024))
echo "$total $up $down"
fi
done
}
#running the scipt only if opennds is working
if [[ $(pgrep opennds) ]] ;then
while read user pw aq; do
username=$user
password=$pw
account_quota=$(($aq+0))
echo "reading $user $pw $aq"
if [ ! -z "$username" ] && [ ! -z "$password" ]; then
echo "checking account"
check_account
echo "done checking account $total $account_quota"
if [ $total -ge $account_quota ]; then
for token in $tokens; do
echo "device $mac username=$user exceeded account quota disconnecting now" >>"/mnt/sda1/out.log"
ndsctlcmd="deauth $token"
do_ndsctl
done
fi
fi
done < "/mnt/sda1/users.txt"
fi