Advertisement
slmingol

shantz-locker.sh

Sep 9th, 2014
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.99 KB | None | 0 0
  1. #!/bin/bash
  2. # Shantz Webcam Autolocker
  3. # A simple, useless script to monitor motion in front of your PC, autolock it when you are away and autounlock it when you come back.
  4. # Kinda useless cuz there is no security and any kind of motion triggers the unlock.
  5. # But still if you like it, then give me a shout at http://tech.shantanugoel.com or http://blog.shantanugoel.com
  6. # The home page for this script is at http://tech.shantanugoel.com/shantz-webcam-autolocker
  7.  
  8. #Usage: ./shantz-locker [-l <LockThreshold>] [-u UnlockThreshold] [-s LockScanInterval] [-t UnlockScanInterval]
  9. #LockThreshold - Determines the threshold (based on no. of samples sans motion) to lock your PC. Default is 5
  10. #UnlockThreshold - Determines the threshold (based on no. of samples with motion) to unlock your PC. Default is 2
  11. #LockScanInterval - How soon to check if there is motion when your PC is in unlocked state. Default interval is 5 seconds
  12. #UnlockScanInterval - How soon to check if there is motion when your PC is in locked state. Default interval is 2 seconds
  13.  
  14.  
  15. motion_sample_cnt=0
  16. motion_sample_max=2
  17. gap_sample_cnt=0
  18. gap_sample_max=5
  19. sleep_time_lock=5
  20. sleep_time_unlock=2
  21.  
  22. while getopts "l:u:s:t:" flag
  23. do
  24.     case $flag in
  25.     l)  echo "Updating Lock Threshold to $OPTARG"
  26.         gap_sample_max=$OPTARG
  27.         ;;
  28.     u)  echo "Updating Unlock Threshold to $OPTARG"
  29.         motion_sample_max=$OPTARG
  30.         ;;
  31.     s)  echo "Updating Lock Scan Interval to $OPTARG"
  32.         sleep_time_lock=$OPTARG
  33.         ;;
  34.     t)  echo "Updating Unlock Scan Interval to $OPTARG"
  35.         sleep_time_unlock=$OPTARG
  36.         ;;
  37.     ?) printf "Usage: %s [-l <LockThreshold>] [-u UnlockThreshold] [-s LockScanInterval] [-t UnlockScanInterval]\n" >&2
  38.         exit 2
  39.         ;;
  40.     esac
  41. done
  42. #echo "$motion_sample_max $gap_sample_max $sleep_time_lock $sleep_time_unlock"
  43.  
  44. wm=`printenv | grep GNOME_DESKTOP_SESSION_ID`
  45. if [ -n "$wm" ]
  46. then
  47. lock_cmd="gnome-screensaver-command -l"
  48. unlock_cmd="gnome-screensaver-command -d"
  49. else
  50. lock_cmd="xscreensaver-command -l"
  51. unlock_cmd="xscreensaver-command -d"
  52. fi
  53.  
  54. state=0
  55. curr_time=0
  56. last_time=0
  57. #0 is unlocked 1 is locked
  58.  
  59. while [ 1 ]
  60. do
  61.     curr_time=`date +%s -r autolock_motion.jpg`
  62.     if [ $curr_time -eq $last_time ]
  63.     then
  64.     gap_sample_cnt=`expr $gap_sample_cnt + 1`
  65.     #echo "increment gap $gap_sample_cnt"
  66.     else
  67.     motion_sample_cnt=`expr $motion_sample_cnt + 1`
  68.     #echo "increment motion $motion_sample_cnt"
  69.     fi
  70.    
  71.     if [ $gap_sample_cnt -ge $gap_sample_max ]
  72.     then
  73.     gap_sample_cnt=0
  74.     motion_sample_cnt=0
  75.     if [ $state == 0 ]
  76.     then
  77.     state=1
  78.     `echo $lock_cmd`
  79.     #else
  80.     #echo "reset motion ctr"
  81.     fi
  82.     fi
  83.  
  84.     if [ $motion_sample_cnt -ge $motion_sample_max ]
  85.     then
  86.     motion_sample_cnt=0
  87.     gap_sample_cnt=0
  88.     if [ $state == 1 ]
  89.     then
  90.     state=0
  91.     `echo $unlock_cmd`
  92.     #else
  93.     #echo "reset gap ctr"
  94.     fi
  95.     fi
  96.    
  97.     if [ $state == 0 ]
  98.     then
  99.     sleep $sleep_time_lock
  100.     #echo $sleep_time_lock
  101.     else
  102.     sleep $sleep_time_unlock
  103.     #echo $sleep_time_unlock
  104.     fi
  105.  
  106.     last_time=$curr_time
  107. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement