Previous Page

nihilist - 00 / 00 / 00

Physical Surveillance Setup

In this tutorial, we're going to look at how we can have some physical security on a raspberry pi with just a few simple usb webcams.

Initial Setup

First plug the usb webcam to the raspberry pi device, and check if it is being recognised by the OS:


#plugging in one webcam
[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ lsusb
Bus 002 Device 002: ID 1058:2621 Western Digital Technologies, Inc. Elements 2621
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:0807 Logitech, Inc. Webcam B500
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub


#After plugging a second webcam
[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ lsusb | grep -i webcam
Bus 001 Device 003: ID 046d:0807 Logitech, Inc. Webcam B500
Bus 001 Device 004: ID 0c45:89a0 Microdia JOYACCESS JA-Webcam
	
[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ apt install v4l2-ctl -y

[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ v4l2-ctl --list-devices
bcm2835-codec-decode (platform:bcm2835-codec):
        /dev/video10
        /dev/video11
        /dev/video12
        /dev/video18
        /dev/video31
        /dev/media1

bcm2835-isp (platform:bcm2835-isp):
        /dev/video13
        /dev/video14
        /dev/video15
        /dev/video16
        /dev/video20
        /dev/video21
        /dev/video22
        /dev/video23
        /dev/media2
        /dev/media3

rpivid (platform:rpivid):
        /dev/video19
        /dev/media0

JOYACCESS JA-Webcam: JOYACCESS  (usb-0000:01:00.0-1.3):
        /dev/video2
        /dev/video3
        /dev/media5

UVC Camera (046d:0807) (usb-0000:01:00.0-1.4):
        /dev/video0
        /dev/video1
        /dev/media4

Now that we know it is being recognised, we can start recording when it detects movement, to do that we use the motion package:


[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ apt install motion
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  ffmpeg fontconfig libaacs0 libass9 libasyncns0 libavc1394-0 libavcodec58 libavdevice58 libavfilter7 libavformat58
  libavresample4 libavutil56 libbdplus0 libbluray2 libbs2b0 libcaca0 libcairo-gobject2 libcairo2 libcdio-cdda2
  libcdio-paranoia2 libcdio19 libchromaprint1 libcodec2-0.9 libdatrie1 libdc1394-25 libdrm-amdgpu1 libdrm-nouveau2
  libdrm-radeon1 libepoxy0 libflite1 libgbm1 libgdk-pixbuf-2.0-0 libgdk-pixbuf2.0-bin libgdk-pixbuf2.0-common libgl1
  libgl1-mesa-dri libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgme0 libgraphite2-3 libgsm1 libharfbuzz0b
  libiec61883-0 libjack-jackd2-0 liblilv-0-0 libllvm11 libmariadb3 libmicrohttpd12 libmp3lame0 libmpg123-0 libmysofa1
  libnorm1 libopenal-data libopenal1 libopenmpt0 libopus0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0
  libpgm-5.3-0 libpixman-1-0 libpocketsphinx3 libpostproc55 libpq5 libpulse0 librabbitmq4 libraw1394-11 librsvg2-2
  librsvg2-common librubberband2 libsdl2-2.0-0 libsensors-config libsensors5 libserd-0-0 libshine3 libsnappy1v5
  libsndfile1 libsndio7.0 libsodium23 libsord-0-0 libsoxr0 libspeex1 libsphinxbase3 libsratom-0-0 libsrt1.4-gnutls
  libssh-gcrypt-4 libswresample3 libswscale5 libthai-data libthai0 libtheora0 libtwolame0 libudfread0 libva-drm2
  libva-x11-2 libva2 libvdpau-va-gl1 libvdpau1 libvidstab1.1 libvorbisenc2 libvorbisfile3 libvpx6 libvulkan1
  libwavpack1 libwayland-client0 libwayland-cursor0 libwayland-egl1 libwayland-server0 libx11-xcb1 libx264-160
  libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-render0 libxcb-shape0 libxcb-shm0
  libxcb-sync1 libxcb-xfixes0 libxcursor1 libxdamage1 libxfixes3 libxi6 libxinerama1 libxkbcommon0 libxrandr2
  libxrender1 libxshmfence1 libxss1 libxv1 libxvidcore4 libxxf86vm1 libz3-4 libzmq5 libzvbi-common libzvbi0
  mariadb-common mesa-va-drivers mesa-vdpau-drivers mesa-vulkan-drivers mysql-common ocl-icd-libopencl1
  pocketsphinx-en-us shared-mime-info va-driver-all vdpau-driver-all x11-common
Suggested packages:
  ffmpeg-doc libbluray-bdj jackd2 libportaudio2 opus-tools pulseaudio libraw1394-doc librsvg2-bin xdg-utils
  lm-sensors serdi sndiod sordi speex default-mysql-client postgresql-client opencl-icd
  nvidia-legacy-390xx-vdpau-driver nvidia-legacy-340xx-vdpau-driver
The following NEW packages will be installed:
  ffmpeg fontconfig libaacs0 libass9 libasyncns0 libavc1394-0 libavcodec58 libavdevice58 libavfilter7 libavformat58
  libavresample4 libavutil56 libbdplus0 libbluray2 libbs2b0 libcaca0 libcairo-gobject2 libcairo2 libcdio-cdda2
  libcdio-paranoia2 libcdio19 libchromaprint1 libcodec2-0.9 libdatrie1 libdc1394-25 libdrm-amdgpu1 libdrm-nouveau2
  libdrm-radeon1 libepoxy0 libflite1 libgbm1 libgdk-pixbuf-2.0-0 libgdk-pixbuf2.0-bin libgdk-pixbuf2.0-common libgl1
  libgl1-mesa-dri libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgme0 libgraphite2-3 libgsm1 libharfbuzz0b
  libiec61883-0 libjack-jackd2-0 liblilv-0-0 libllvm11 libmariadb3 libmicrohttpd12 libmp3lame0 libmpg123-0 libmysofa1
  libnorm1 libopenal-data libopenal1 libopenmpt0 libopus0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0
  libpgm-5.3-0 libpixman-1-0 libpocketsphinx3 libpostproc55 libpq5 libpulse0 librabbitmq4 libraw1394-11 librsvg2-2
  librsvg2-common librubberband2 libsdl2-2.0-0 libsensors-config libsensors5 libserd-0-0 libshine3 libsnappy1v5
  libsndfile1 libsndio7.0 libsodium23 libsord-0-0 libsoxr0 libspeex1 libsphinxbase3 libsratom-0-0 libsrt1.4-gnutls
  libssh-gcrypt-4 libswresample3 libswscale5 libthai-data libthai0 libtheora0 libtwolame0 libudfread0 libva-drm2
  libva-x11-2 libva2 libvdpau-va-gl1 libvdpau1 libvidstab1.1 libvorbisenc2 libvorbisfile3 libvpx6 libvulkan1
  libwavpack1 libwayland-client0 libwayland-cursor0 libwayland-egl1 libwayland-server0 libx11-xcb1 libx264-160
  libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-render0 libxcb-shape0 libxcb-shm0
  libxcb-sync1 libxcb-xfixes0 libxcursor1 libxdamage1 libxfixes3 libxi6 libxinerama1 libxkbcommon0 libxrandr2
  libxrender1 libxshmfence1 libxss1 libxv1 libxvidcore4 libxxf86vm1 libz3-4 libzmq5 libzvbi-common libzvbi0
  mariadb-common mesa-va-drivers mesa-vdpau-drivers mesa-vulkan-drivers motion mysql-common ocl-icd-libopencl1
  pocketsphinx-en-us shared-mime-info va-driver-all vdpau-driver-all x11-common
0 upgraded, 151 newly installed, 0 to remove and 0 not upgraded.
Need to get 115 MB of archives.
After this operation, 322 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
	

To then setup motion you can read through the documentation here


[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ vim /etc/motion/motion.conf

[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ cat /etc/motion/motion.conf
# Rename this distribution example file to motion.conf
#
# This config file was generated by motion 4.3.2
# Documentation:  /usr/share/doc/motion/motion_guide.html
#
# This file contains only the basic configuration options to get a
# system working.  There are many more options available.  Please
# consult the documentation for the complete list of all options.
#

############################################################
# System control configuration parameters
############################################################

# Start in daemon (background) mode and release terminal.
daemon off

# Start in Setup-Mode, daemon disabled.
setup_mode off

# File to store the process ID.
; pid_file value

# File to write logs messages into.  If not defined stderr and syslog is used.
log_file /var/log/motion/motion.log

# Level of log messages [1..9] (EMG, ALR, CRT, ERR, WRN, NTC, INF, DBG, ALL).
log_level 6

# Target directory for pictures, snapshots and movies
target_dir /mnt/VAULT/cameras

# Video device (e.g. /dev/video0) to be used for capturing.
;videodevice /dev/video2

# Parameters to control video device.  See motion_guide.html
; vid_control_params value

# The full URL of the network camera stream.
; netcam_url value

# Name of mmal camera (e.g. vc.ril.camera for pi camera).
; mmalcam_name value

# Camera control parameters (see raspivid/raspistill tool documentation)
; mmalcam_control_params value

############################################################
# Image Processing configuration parameters
############################################################

# Image width in pixels.
width 1920

# Image height in pixels.
height 1080

# Maximum number of frames to be captured per second.
framerate 2

# Text to be overlayed in the lower left corner of images
text_left Desk

# Text to be overlayed in the lower right corner of images.
text_right %Y-%m-%d\n%T-%q

############################################################
# Motion detection configuration parameters
############################################################

# Always save pictures and movies even if there was no motion.
emulate_motion off

# Threshold for number of changed pixels that triggers motion.
threshold 1500

# Noise threshold for the motion detection.
; noise_level 32

# Despeckle the image using (E/e)rode or (D/d)ilate or (l)abel.
despeckle_filter EedDl

# Number of images that must contain motion to trigger an event.
minimum_motion_frames 1

# Gap in seconds of no motion detected that triggers the end of an event.
event_gap 60

# The number of pre-captured (buffered) pictures from before motion.
pre_capture 10

# Number of frames to capture after motion is no longer detected.
post_capture 10

############################################################
# Script execution configuration parameters
############################################################

# Command to be executed when an event starts.
; on_event_start value

# Command to be executed when an event ends.
; on_event_end value

# Command to be executed when a movie file is closed.
; on_movie_end value

############################################################
# Picture output configuration parameters
############################################################

# Output pictures when motion is detected
picture_output off

# File name(without extension) for pictures relative to target directory
picture_filename %Y%m%d%H%M%S-%q

############################################################
# Movie output configuration parameters
############################################################

# Create movies of motion events.
movie_output on

# Maximum length of movie in seconds.
movie_max_time 60

# The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
movie_quality 50

# Container/Codec to used for the movie. See motion_guide.html
movie_codec mkv

# File name(without extension) for movies relative to target directory
movie_filename %t-%v-%Y%m%d%H%M%S

############################################################
# Webcontrol configuration parameters
############################################################

# Port number used for the webcontrol.
webcontrol_port 8080

# Restrict webcontrol connections to the localhost.
webcontrol_localhost off

# Type of configuration options to allow via the webcontrol.
webcontrol_parms 0

############################################################
# Live stream configuration parameters
############################################################

# The port number for the live stream.
stream_port 8081

# Restrict stream connections to the localhost.
stream_localhost off

##############################################################
# Camera config files - One for each camera.
##############################################################
camera /etc/motion/camera1.conf
camera /etc/motion/camera2.conf
; camera /usr/etc/motion/camera3.conf
; camera /usr/etc/motion/camera4.conf

##############################################################
# Directory to read '.conf' files for cameras.
##############################################################
; camera_dir /usr/etc/motion/conf.d

[ 10.0.0.223/16 ] [ /dev/pts/0 ] [/etc/motion]
→ cat camera1.conf
# /usr/etc/motion/camera1.conf
#
# This config file was generated by motion 4.3.2

###########################################################
# Configuration options specific to camera 1
############################################################
# User defined name for the camera.
camera_name cam1

# Numeric identifier for the camera.
camera_id 101

# The full URL of the network camera stream.
#netcam_url rtsp://yourcamera1ip:port/camera/specific/url
videodevice /dev/video0

#framerate
framerate 5

# Image width in pixels.
width 800

# Image height in pixels.
height 600

# Text to be overlayed in the lower left corner of images
text_left Desk

#filepath
target_dir /mnt/VAULT/cameras

# File name(without extension) for movies relative to target directory
movie_filename DESK_%t-%v-%Y-%m-%d_%H:%M:%S


[ 10.0.0.223/16 ] [ /dev/pts/0 ] [/etc/motion]
→ cat camera2.conf
# /usr/etc/motion/camera1.conf
#
# This config file was generated by motion 4.3.2

###########################################################
# Configuration options specific to camera 1
############################################################
# User defined name for the camera.
camera_name cam2

# Numeric identifier for the camera.
camera_id 102

# The full URL of the network camera stream.
#netcam_url rtsp://yourcamera1ip:port/camera/specific/url
videodevice /dev/video2

#framerate
framerate 5

# Image width in pixels.
width 800

# Image height in pixels.
height 600

# Text to be overlayed in the lower left corner of images
text_left Staircase

#filepath
target_dir /mnt/VAULT/cameras

# File name(without extension) for movies relative to target directory
movie_filename STAIRCASE_%t-%v-%Y-%m-%d_%H:%M:%S

[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ vim /etc/default/motion

[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ cat /etc/default/motion
start_motion_daemon=yes
	

Then start motion:


[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ systemctl stop motion

[term1]
[ 10.0.0.223/16 ] [ /dev/pts/2 ] [~]
→ motion
[0:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[0:motion] [NTC] [ALL] create_path: creating directory /var/log/motion
[0:motion] [NTC] [ALL] motion_startup: Logging to file (/var/log/motion/motion.log)

[term2]
[ 10.0.0.223/16 ] [ /dev/pts/1 ] [/mnt/VAULT/cameras]
→ tail -f /var/log/motion/motion.log
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_select: Supported palettes:
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_select: (0) MJPG (Motion-JPEG)
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_select: (1) YUYV (YUYV 4:2:2)
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_set: Testing palette YUYV (800x600)
[2:ml2:cam2] [WRN] [VID] [Dec 19 17:31:51] v4l2_pixfmt_set: Adjusting resolution from 800x600 to 640x480.
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_set: Using palette YUYV (640x480)
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_select: Selected palette YUYV
[2:ml2:cam2] [NTC] [ALL] [Dec 19 17:31:51] image_ring_resize: Resizing pre_capture buffer to 1 items
[1:ml1:cam1] [NTC] [ALL] [Dec 19 17:31:52] image_ring_resize: Resizing pre_capture buffer to 11 items
[2:ml2:cam2] [NTC] [ALL] [Dec 19 17:31:52] image_ring_resize: Resizing pre_capture buffer to 11 items
[1:ml1:cam1] [NTC] [EVT] [Dec 19 17:31:56] event_newfile: File of type 8 saved to: /mnt/VAULT/cameras/DESK_101-01-2022-12-19_17:31:54.mkv
[1:ml1:cam1] [NTC] [ALL] [Dec 19 17:31:56] motion_detected: Motion detected - starting event 1

[term3]
[ 10.0.0.223/16 ] [ /dev/pts/2 ] [/mnt/VAULT/cameras]
→ watch -n1 ls -lash
	

System-wise that's how you will look for camera events, you will see your recordings appear in the /mnt/VAULT/cameras/ folder (thats configurable in the config files) like so:


[term2]
[ 10.0.0.223/16 ] [ /dev/pts/1 ] [/mnt/VAULT/cameras]
→ tail -f /var/log/motion/motion.log
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_select: Supported palettes:
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_select: (0) MJPG (Motion-JPEG)
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_select: (1) YUYV (YUYV 4:2:2)
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_set: Testing palette YUYV (800x600)
[2:ml2:cam2] [WRN] [VID] [Dec 19 17:31:51] v4l2_pixfmt_set: Adjusting resolution from 800x600 to 640x480.
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_set: Using palette YUYV (640x480)
[2:ml2:cam2] [NTC] [VID] [Dec 19 17:31:51] v4l2_pixfmt_select: Selected palette YUYV
[2:ml2:cam2] [NTC] [ALL] [Dec 19 17:31:51] image_ring_resize: Resizing pre_capture buffer to 1 items
[1:ml1:cam1] [NTC] [ALL] [Dec 19 17:31:52] image_ring_resize: Resizing pre_capture buffer to 11 items
[2:ml2:cam2] [NTC] [ALL] [Dec 19 17:31:52] image_ring_resize: Resizing pre_capture buffer to 11 items
[1:ml1:cam1] [NTC] [EVT] [Dec 19 17:31:56] event_newfile: File of type 8 saved to: /mnt/VAULT/cameras/DESK_101-01-2022-12-19_17:31:54.mkv
[1:ml1:cam1] [NTC] [ALL] [Dec 19 17:31:56] motion_detected: Motion detected - starting event 1
[1:ml1:cam1] [NTC] [ALL] [Dec 19 17:32:56] mlp_actions: End of event 1
[1:ml1:cam1] [NTC] [EVT] [Dec 19 17:34:24] event_newfile: File of type 8 saved to: /mnt/VAULT/cameras/DESK_101-02-2022-12-19_17:34:22.mkv
[1:ml1:cam1] [NTC] [ALL] [Dec 19 17:34:24] motion_detected: Motion detected - starting event 2
[1:ml1:cam1] [NTC] [ALL] [Dec 19 17:35:24] mlp_actions: End of event 2

[term3]
Every 1.0s: ls -lash                                                             

total 3.9M
 12K drwxr-xr-x  2 root root  12K Dec 19 17:34 .
4.0K drwxr-xr-x 17 root root 4.0K Dec 18 21:38 ..
3.9M -rw-r--r--  1 root root 3.9M Dec 19 17:35 DESK_101-02-2022-12-19_17:34:22.mkv

In realtime you can see the live video feed on the webservice at port 8080:

Here as you can see I have a webcam in my staircase (right) and one in my room (left).

To see the output of the files from your web-browser you can just run a nginx service into where the mp4 files are stored, and have autoindex on to freely browse them:


apt install nginx
rm /etc/nginx/sites-*/default
vim /etc/nginx/sites-available/cameras.conf

[ 10.0.0.223/16 ] [ /dev/pts/0 ] [/etc/nginx/sites-available]
→ cat cameras.conf
server {
        listen 80;
        autoindex on;
        root /mnt/VAULT/cameras/;

}	

[ 10.0.0.223/16 ] [ /dev/pts/0 ] [/etc/nginx/sites-available]
→ ln -s /etc/nginx/sites-available/cameras.conf /etc/nginx/sites-enabled

[ 10.0.0.223/16 ] [ /dev/pts/0 ] [/etc/nginx/sites-available]
→ systemctl restart nginx

Now let's look at how we can send a mail whenever we have a new file being created in /mnt/VAULT/cameras/. For this part you will need to have a functionning mail server, to know how to setup one please see this tutorial i made. My mail server is at 10.0.0.222


[ 10.0.0.223/16 ] [ /dev/pts/1 ] [~]
→ apt install ssmtp 
	

Then edit the config file to send to the correct smtp server:


vim /etc/ssmtp/ssmtp.conf
	
[ 10.0.0.223/16 ] [ /dev/pts/1 ] [~]
→ cat /etc/ssmtp/ssmtp.conf

mailhub=mail.nihilism.network:587
useSTARTTLS=YES
AuthUser=surveillance
AuthPass=D8410983292138128812732167397216736217362167927F

Then make sure the user exists on the smtp server:


root@mail-nihilism:~# useradd -G mail -m surveillance
root@mail-nihilism:~# id surveillance
uid=1003(surveillance) gid=1003(surveillance) groups=1003(surveillance),8(mail)
root@mail-nihilism:~# passwd surveillance
New password:
Retype new password:
passwd: password updated successfully
	

And then just send the mail:


[ 10.0.0.223/16 ] [ /dev/pts/1 ] [~]
→ echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv nihilist@nihilism.network
[<-] 220 nihilism.network ESMTP Postfix (Debian/GNU)
[->] EHLO raspberrypi
[<-] 250 CHUNKING
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO raspberrypi
[<-] 250 CHUNKING
[->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6
[->] c3VydmVpbGxhbmNl
[<-] 334 UGFzc3dvcmQ6
[<-] 235 2.7.0 Authentication successful
[->] MAIL FROM:oot@raspberrypi>
[<-] 250 2.1.0 Ok
[->] RCPT TO:<nihilist@nihilism.network>
[<-] 250 2.1.5 Ok
[->] DATA
[<-] 354 End data with <CR><LF>.<CR><LF>
[->] Received: by raspberrypi (sSMTP sendmail emulation); Tue, 20 Dec 2022 16:09:09 +0000
[->] From: "root" <root@raspberrypi>
[->] Date: Tue, 20 Dec 2022 16:09:09 +0000
[->] Test message from Linux server using ssmtp
[->]
[->] .
[<-] 250 2.0.0 Ok: queued as 2B5D31202BB
[->] QUIT
[<-] 221 2.0.0 Bye

	

Then here we see that we recieved it:

Now let's make it a little more sophisticated, i want it to send the mp4 recording in the mail, and only send it once it is a valid mp4 file (you check it with ffmpeg basically):


[ 10.0.0.223/16 ] [ /dev/pts/0 ] [~]
→ cat watchdir.sh
#!/bin/bash

echo "[+] Watching for new recordings..."
while true;do
        find /mnt/VAULT/cameras/ -name *.mp4 > /tmp/oldfiles
        sleep 1
        find /mnt/VAULT/cameras/ -name *.mp4 > /tmp/newfiles


        #comm -13 <(echo $newfiles) <(echo $oldfiles)
        newfiles=$(diff -u /tmp/oldfiles /tmp/newfiles | grep -E "^\+" | grep -v "+++" | tr -d '+')

        for line in $newfiles
        do
                file $line
                echo $line Sending the mail...
                echo "checking the file: $(ffmpeg -v error -i $line -f null - | wc -l)"
                while [ "$(ffmpeg -v error -i $line -f null - | wc -l)" -gt 0 ] ; do
                        echo "checking the file: $(ffmpeg -v error -i $line -f null - | wc -l)"
                        echo "waiting for mp4 file to finish..."
                        sleep 1
                done
                filename=$(basename $line)
                echo -e "to: nihilist@nihilism.network\nsubject: Movement Detected\nMovement detected at Home"| (cat - && uuencode $line $filename) | ssmtp nihilist@nihilism.network
                echo "[+] Watching for new recordings..."
        done
done

I also want motion to activate only when i shutdown my mainpc, because that is usually when i leave my house. So i have the following script:


[ 10.0.0.223/16 ] [ /dev/pts/0 ] [~]
→ vim monitor_mainpc.sh

[ 10.0.0.223/16 ] [ /dev/pts/0 ] [~]
→ cat monitor_mainpc.sh
#!/bin/bash

counter=1
echo '[+] mainpc active, no need for surveillance...'
while true; do
        timeout 10 ping -c1 10.0.0.10 &>/dev/null
        if [ $? -eq 0 ]; then
                if [ $counter -eq 0 ];
                then
                    echo '[+] mainpc active, no need for surveillance...'
                    systemctl stop motion
                    systemctl status motion
                    counter=1 #dont stop motion more than once, until mainpc goes inactive
                    sleep 3
                else
                    sleep 3
                fi
        else
                if [ $counter -gt 0 ];
                then
                        echo '[+] mainpc inactive, enabling surveillance...'
                        systemctl start motion
                        systemctl status motion
                        counter=0 #dont start motion more than once, until mainpc goes active
                        sleep 10
                else
                        sleep 3
                fi
        fi
done
	

For both scripts, you need a systemd service to launch them, and also do not let motion start at bootup, as it will be handled by monitor_mainpc.sh


[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ systemctl disable motion

[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ vim /etc/systemd/system/watchdir.service

[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ cat /etc/systemd/system/watchdir.service
[Unit]
Description=watchdir
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/root/watchdir.sh
ExecStop=kill -9 $(pidof /root/watchdir.sh)
Restart=always

[Install]
WantedBy=multi-user.target


[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ vim /etc/systemd/system/monitor_mainpc.service

[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ cat /etc/systemd/system/monitor_mainpc.service
[Unit]
Description=monitor_mainpc
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/root/monitor_mainpc.sh
ExecStop=kill -9 $(pidof /root/monitor_mainpc.sh)
Restart=always

[Install]
WantedBy=multi-user.target

[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ systemctl daemon-reload

[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ systemctl enable --now monitor_mainpc watchdir
Created symlink /etc/systemd/system/multi-user.target.wants/monitor_mainpc.service → /etc/systemd/system/monitor_mainpc.service.
Created symlink /etc/systemd/system/multi-user.target.wants/watchdir.service → /etc/systemd/system/watchdir.service.

[ 10.0.0.223/16 ] [ /dev/pts/3 ] [~]
→ systemctl status monitor_mainpc watchdir
● monitor_mainpc.service - monitor_mainpc
     Loaded: loaded (/etc/systemd/system/monitor_mainpc.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-12-25 18:32:27 GMT; 6s ago
   Main PID: 3253 (monitor_mainpc.)
      Tasks: 2 (limit: 4915)
        CPU: 31ms
     CGroup: /system.slice/monitor_mainpc.service
             ├─3253 /bin/bash /root/monitor_mainpc.sh
             └─3326 sleep 3

Dec 25 18:32:27 raspberrypi systemd[1]: Started monitor_mainpc.
Dec 25 18:32:27 raspberrypi monitor_mainpc.sh[3253]: [+] mainpc active, no need for surveillance...

● watchdir.service - watchdir
     Loaded: loaded (/etc/systemd/system/watchdir.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-12-25 18:32:27 GMT; 6s ago
   Main PID: 3254 (watchdir.sh)
      Tasks: 2 (limit: 4915)
        CPU: 223ms
     CGroup: /system.slice/watchdir.service
             ├─3254 /bin/bash /root/watchdir.sh
             └─3357 /bin/bash /root/watchdir.sh

Dec 25 18:32:27 raspberrypi systemd[1]: Started watchdir.
Dec 25 18:32:27 raspberrypi watchdir.sh[3254]: [+] Watching for new recordings...
	

Nihilism

Until there is Nothing left.

About nihilist

Donate XMR: 8AUYjhQeG3D5aodJDtqG499N5jXXM71gYKD8LgSsFB9BUV1o7muLv3DXHoydRTK4SZaaUBq4EAUqpZHLrX2VZLH71Jrd9k8


Contact: nihilist@nihilism.network (PGP)