forked from segalion/securitasdirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecuritas
144 lines (135 loc) · 4.95 KB
/
securitas
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# This is a prototipe command-line tool to interact with Verisure Alarm
# service in Spain (maybe France?). Has been implemented with reverse
# engineering over this web services (as browser do).
#
# Change NICK and PASSWD with your web credentials
# You can get state (status), arm (day, night, house), disarm (unlock)
# and take photos (photo_request)
#
# This code is under MIT licence by segalion at gmail. Use at your own risk.
# release 2.1 (03/10/2018)
NICK="[email protected]"
PASSWD="change4yourpasswd"
HOST="https://customers.securitasdirect.es"
INSTALL="es/installations"
COOKIE=".securitas.cookie"
FILEOUT="securitas.state"
FILELOG="securitas.log"
CURL="curl -sL -m 10 -c $COOKIE -b $COOKIE"
between () {
echo $tmp | grep -o -P '(?<='"$1"').*?(?='"$2"')'
}
eprint () {
echo -en "\r \r$*">&2
# log $*
}
print () {
eprint ""; echo -e "$*"
# log $*
}
log (){
echo -en "$(date +'%d)%H:%M:%S'):$*\n" >> $FILELOG
}
cd "${0%/*}"
case $1 in
"") option="";;
status|unlock|day|night|house|log) option="panel/$1";;
armed_away) option="panel/house";;
armed_home) option="panel/day";;
armed_night) option="panel/night";;
disarmed) option="panel/unlock";;
photo_request) option="";;
*) eprint "$0 [status|disarmed|armed_away|armed_home|armed_ningt|photo_request|log]"; exit -1;;
esac
eprint "connecting"
# tmp=$($CURL $HOST/$INSTALL/status?filter=1)
for i in {0..2}; do
tmp=$($CURL -i "$HOST/$INSTALL/status?filter=0&download=1")
inst=$( between 'Content-Disposition: attachment; filename="file' '.csv"')
if [ -z "$inst" ]; then
token=$( between 'authenticity_token" name="csrf-param" /> <meta content="' '" name="csrf-token"' )
# token=$(echo $tmp | grep -o -P '(?<=authenticity_token" name="csrf-param" /> <meta content=").*(?=" name="csrf-token")')
# inst=$( between 'Nº INST. <strong>' '</strong></span>' )
# echo -e "\ninst=$inst\n"
# echo $tmp > securitas.log
eprint "login.$i"
sleep $i
if [ -z "$token" ]; then exit -1;fi
tmp=$($CURL $HOST/es/login/es -K- <<< "-d \"verisure_rsi_login[nick]=$NICK&verisure_rsi_login[passwd]=$PASSWD&authenticity_token=$token&button=&utf8=\✓\" -e \"$HOST/\"")
else
eprint "inst:$inst"
out=$(echo -e "$tmp" | awk -F ";" '($1 ~ /^[0-9]/) {print $0}' )
echo "$out" > $FILEOUT
break
fi
done
if [ -z "$inst" ]; then rm $COOKIE; exit -2; fi
if [ "$option" = "panel/log" ]; then eprint ""; head -10 $FILEOUT; exit; fi
if [ -z "$option" ]; then
# PARSE CSV
echo "$out" | while IFS= read -r line; do
#SPLIT ITEMS
IFS=';' read -a w <<<$line
case ${w[1]} in
Cerrar|Conectado) state="armed_away";;
"Armado día"|"Cerrar día") state="armed_home";;
"Armado noche"|"Cerrar noche") state="armed_night";;
"Sabotaje/manipulación"|Alarma) state="triggered";;
Desconectado|Abrir) state="disarmed";;
#"Error procesando conexión de alarma"|"Petición de foto"|"Comprobacion de estado") state="";;
esac
if [ -n "$state" ]; then
json='{"last_triggered": "'${w[0]}'", "state": "'$state'", "changed_by": "'${w[4]}${w[5]}'" }'
print $json
break
fi
done
exit 0
fi
tmp=$($CURL $HOST/$INSTALL/$inst/$option)
token=$( between 'name="authenticity_token" type="hidden" value="' '" /></div>' )
for i in {0..2}; do
eprint "$i.$option"
sleep $i
tmp=$($CURL $HOST/$INSTALL/$inst/$option -d "utf8=\✓" -d "authenticity_token=$token")
# token=$( between 'name="authenticity_token" type="hidden" value="' '" /></div>' )
# job_id=`echo $tmp | grep -o -P '(?<={"job_id":").*(?="})'`
job_id=$( between '{"job_id":"' '"}' )
if [ -n "$job_id" ]; then break;fi
done
if [ -n "$job_id" ]; then
for j in {0..6}; do
eprint "$i.$option(w:$t.5)"
t=$((3-$j))
sleep ${t#-}.5
tmp=$($CURL $HOST/es/remote/job_status/$job_id)
#state=`echo $tmp | grep -o -P '(?<="message":{"status":0,"message":").*(?="}})'`
message=`echo $tmp | grep -o -P '(?<=,"message":").*(?="})'`
if [ -n "$message" ]; then break; fi
done
fi
eprint "{$message}"
case $message in
"Todos "*" conectados") state="armed_away";;
"Tu Alarma "*" modo Parcial d"*) state="armed_home";;
"Tu alarma "*" modo parcial n"*) state="armed_night";;
"Tu alarma "*" perimetral"*) state="armed_custom_bypass";;
*"desconectada"*) state="disarmed";;
*" desconecta tu Alarma "*) state="armed_?";; # to force one hipotetical armed state
esac
exit_code=-100
if [ -n "$state" ]; then
json='{"last_triggered": "'$(date +"%d-%m-%Y %H:%M ")'", "state": "'$state'", "changed_by": "VERISURE WEB" }'
# print $json
log $json
print $state
exit_code=0
fi
exit $exit_code
# Todos los dispositivos de tu Alarma han sido conectados
# No ha sido posible activar este modo porque tienes otro activado. Por favor, desconecta tu Alarma y repite la operación.
# Tu Alarma ha sido desconectada
# Tu Alarma está conectada en modo Parcial día
# Tu alarma esta conectada en modo parcial noche
# show-photos?notification_id=201618076