Skip to content

Instantly share code, notes, and snippets.

@santrancisco
Last active August 19, 2022 21:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santrancisco/9d14e0105316cfa15f98f0fa7c35f8a0 to your computer and use it in GitHub Desktop.
Save santrancisco/9d14e0105316cfa15f98f0fa7c35f8a0 to your computer and use it in GitHub Desktop.
A simple script to take screenshot and archive it.
#!/bin/bash
# Requirement:
# Linux: xdotool + convert(imagemagik) + flameshot
# Mac: xdotool + imagemagick
# Print out each commands and bail if there is an error
set -ex
CODELOC=$HOME/.sanscreenie
SCRLOC="$CODELOC/screenshots"
ARCV="$CODELOC/archive"
mkdir -p $SCRLOC
mkdir -p $ARCV
TEMPHASH="$CODELOC/mouseloc.md5"
TEMPFILE="$SCRLOC/temp.jpg"
# Make sure this file exists
touch $TEMPHASH
function takescreenshotmac {
numberofdisplay=$(/usr/sbin/system_profiler SPDisplaysDataType | /usr/bin/grep Resolution: | /usr/bin/wc -l)
for n in `seq 1 $numberofdisplay`
do
/usr/sbin/screencapture -x -D $n -t jpg $TEMPFILE
convert $TEMPFILE -strip -depth 5 -quality 50 +dither -colors 8 $SCRLOC/`date +"%Y-%m-%d_%H-%M-%S"`-D$n.png
done
# screencapture -t jpg test.jpg
}
function takescreenshotlinux {
# Get the current mouse location hash
CURRENTHASH=`xdotool getmouselocation | md5sum`
# Get the previous mouse location hash
PREVIOUSHASH=$(cat $TEMPHASH)
# If mouse moved, take a screenshot
if [[ "$CURRENTHASH" != "$PREVIOUSHASH" ]]; then
echo "$CURRENTHASH" > $TEMPHASH
echo "Mouse moved since last screenshot - Saving a new one!"
# Take high resolution screenshot - Cost a lot of diskspace
# flameshot full -p $SCRLOC
# Take low resolution screenshot :)
flameshot full -r | convert png:- +dither -colors 8 $SCRLOC/`date +"%Y-%m-%d_%H-%M-%S"`.png
fi
}
## Create gpg encrypted archive
function archive {
## Create encrypted achive if there screenshots taken
if [[ $1 == "" ]];then
echo "Please provide a recipient email for gpg encrypt function"
exit
fi
cd $SCRLOC
n=$(find . -type f -name "*.png" | wc -l)
if (( $n > 0 )); then
echo $n
ls *
tar -c *.png | /usr/local/bin/gpg --output $ARCV/`date +'%s'`.tar.gpg --encrypt --recipient $1
## Delete all screenshots
rm $SCRLOC/*.png
rm $SCRLOC/*.jpg
fi
# To decrypt the screnshot archive, we use the following command:
# gpg --decrypt 1605006952.tar.gpg | tar xv
}
function help {
echo "$0 <task> <args>"
echo "Tasks: "
compgen -A function | egrep -v 'cleanup|callmeusage' |cat -n
}
# To quickly use this script to record screen every 5 seconds:
# while [ 1 ]; do screenrecord schedulerun; sleep 5; done
function schedulerun {
if [ -f /tmp/screenrec ]; then
if [ "$(uname)" == "Darwin" ]; then
takescreenshotmac
else
takescreenshotlinux
fi
fi
}
# execute the function and arguments pass to this script.
# If nothing provided, run help function
${@:-help}
@santrancisco
Copy link
Author

santrancisco commented Nov 10, 2020

./record.sh takescreenshot will take screenshot if the mouse has moved since its last position
./record.sh archive your@email.address will gpg encrypts screenshot files using your public key and thus only you can decrypt it later.

This script is useful in various occasions, for example, when you work on an incident and need to record your actions or have a way for you to go back later to backtrack your steps when writing an incident report, you can run this script using linux watch command to take screenshot every second to take note. The archive script should be run every day or more frequent, depends on how paranoid you are leaving screenshots of your activities on your own machine :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment