CCTV Surveillance station
Script that trigger HOME MODE when a device Arrive in LAN.

Script that trigger HOME MODE when a device Arrive in LAN.

With the help of Ricky and a this link

###to unstall nmap
###sudo -s
###synogear install
Functions
###Check HTTP Response
function _cleanupCookies ()
{
rm $COOKIESFILE
}
function _checkSynoResponse ()
{
if [ $1 != ‘{“success”:true}’ ]
then
echo “$2 failed: “$RESPONSE
_cleanupCookies
exit -1
fi
}
homemode=false;
currenttime=$(date +%H)
currenttime=echo $currenttime|sed 's/^0*//'
echo “Current time: $currenttime”;
if [[ “$currenttime” -lt 6 ]]; then
echo “Never in home mode”;
### in this section is to exit form home mode in the night 00:00 –> 06:00
else
### in this section is to set or not home mode based on device presence
echo "Check Devicies"; for MAC in `/var/packages/DiagnosisTool/target/tool/nmap -sn 192.168.1.0/24 2> /dev/null | grep MAC | awk '{print $3}'`; do echo "Found $MAC"; case $MAC in "XX:XX:XX:XX:XX:XX"|"YY:YY:YY:YY:YY:YY") homemode=true; echo " Found $MAC Listed, SET home mode to $homemode"; esac done
fi
echo “End of Tests, reslt home mode = $homemode”;
SYNO_SS_USER=”APIUSERTEST”
SYNO_SS_PASS=”xxxxxxx”
SYNO_URL=”localhost:5000″
SYNO_CAMERAIDS=”1″
### Internal variables
COOKIESFILE=”$0-cookies-$ID”
ID=”$RANDOM”
# 1.step login
VER=”1″
METHOD=”Login”
echo “http://${SYNO_URL}/webapi/auth.cgi?api=SYNO.API.Auth&method=${METHOD}&version=${VER}&account=${SYNO_SS_USER}&passwd=${SYNO_SS_PASS}&session=SurveillanceStation”
RESPONSE=wget -q --keep-session-cookies --save-cookies $COOKIESFILE -O- "http://${SYNO_URL}/webapi/auth.cgi?api=SYNO.API.Auth&method=${METHOD}&version=${VER}&account=${SYNO_SS_USER}&passwd=${SYNO_SS_PASS}&session=SurveillanceStation"
_checkSynoResponse $RESPONSE $METHOD;
echo “repo= $RESPONSE Meth = $METHOD”;
## 2.Set Home Mode
echo “http://${SYNO_URL}/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=${homemode}”
RESPONSE=wget -q --load-cookies $COOKIESFILE -O- "http://${SYNO_URL}/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=${homemode}"
_checkSynoResponse $RESPONSE $METHOD;
echo “repo= $RESPONSE”;
## 3.step logout
VER=1
METHOD=”Logout”
echo “http://${SYNO_URL}/webapi/auth.cgi?api=SYNO.API.Auth&method=$METHOD&version=${VER}”
RESPONSE=wget -q --load-cookies $COOKIESFILE -O- "http://${SYNO_URL}/webapi/auth.cgi?api=SYNO.API.Auth&method=$METHOD&version=${VER}"
_checkSynoResponse $RESPONSE $METHOD;
echo “repo= $RESPONSE Meth = $METHOD”;
Cleanup
_cleanupCookies
exit