BestTechie Forums: Screenshot Script - BestTechie Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Screenshot Script


#1 User is offline   Michael Mansell 

  • Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 21-November 10

Posted 25 November 2010 - 08:36 PM

For anyone looking for a quick and simple screenshot script in Linux, here's something that I threw together. A few people have found it to be of use, so I figured I'd post it here as well.

Simply change your upload method and provide it with your user/pass or API keys as necessary.

Post a reply if you have any questions.

# Requires:
#	scrot (to take the actual screenshot with)
#	ftp (if using ftp)
#	sshpass (if using ssh)
#	s3cmd (if using s3)
#	imagemagick (is using dropshadow)
#	xsel (to copy the url to the clipboard)
#	curl (used for a few things; imageshack, imgur, url shortners, etc)
# 
# Based on a script from Andrew @ LocalCoast (andrew@localcoast.net)
# If uploading to s3, be sure to run "s3cmd --configure" to give it your API keys


############################
# Screenshot Configuration #
############################
LOCALDIR="$HOME/.grabup/grabs" # Where are local files stored before upload?
DELETELOCAL="false" # Delete the local file once it has been uploaded?  "true" or "false"
DATE=`date +%Y%m%d%H%M%S` # Date format
DROPSHADOW="true" # Use ImageMagick?  "true" or "false"

########################
# Upload Configuration #
########################
DOMAIN="" # Domain (or S3 bucket) used to upload your grabs to
UPLOADMETHOD="" # Your choice of upload method; "ftp", "ssh", "imageshack", "imgur", "s3"
S3_CUSTOMURL="" # Use the "LONGURL" specified below for S3?  Use this if you have a CNAME; "true" or "false"

SERVER_USER="" # Server username
SERVER_PASS="" # Server password

SERVER_GDIR="" # Where grabs are stored on the server (SSH or FTP only)

UPLOAD_APIK="" # API key to be used with imgur, etc

LONGURL="http://$DOMAIN/$DATE.png" # Typically "http://$DOMAIN/$DATE.png".  Blank for ImageShack, Imgur.

##############################
# URL Shortner Configuration #
##############################
URLMETHOD="" # How you want URLs to be copied to your clipboard; "long", "tinyurl", "isgd", or "bitly"
URL_APIU="" # API Key: Required for Bitly: http://bit.ly/a/your_api_key
URL_APIK="" # API Key: Required for Bitly




#############################################################
########## START THE SCREENSHOT AND UPLOAD PROCESS ##########
#############################################################
#############################################################
########## You do not want to edit past here       ##########
##########   unless you know what you are doing    ##########
#############################################################

# Take screenshot
BASENAME="$DATE.png"
FILENAME="$LOCALDIR/$BASENAME"
scrot -s $FILENAME




# Add drop-shadow
if [ "$DROPSHADOW" = "true" ]; then
convert $FILENAME -gravity northwest -background 'rgba(255,255,255,0)' -splice 10x10 \
\( +clone -background gray -shadow 80x12-1-1 \) +swap -background none -mosaic +repage \
\( +clone -background gray -shadow 80x12+5+5 \) +swap -background none -mosaic +repage $FILENAME
fi




# Upload
if [ "$UPLOADMETHOD" = "ftp" ]; then
cd $LOCALDIR
ftp -n -v $DOMAIN << DONEWITHFTP
user $SERVER_USER $SERVER_PASS
cd $SERVER_GDIR
put $BASENAME
bye
DONEWITHFTP
fi

if [ "$UPLOADMETHOD" = "ssh" ]; then
sshpass -p $SSH_PASS scp -P 22 $FILENAME $SERVER_USER@$DOMAIN:$SERVER_GDIR
fi

if [ "$UPLOADMETHOD" = "imageshack" ]; then
LONGURL=`curl -H Expect: -F fileupload="@$FILENAME" -F xml=yes -# "http://www.imageshack.us/index.php" | grep image_link | grep -o  http[^\<]*`
fi

if [ "$UPLOADMETHOD" = "imgur" ]; then
LONGURL=`curl -F "image=@$FILENAME" -F "key=$UPLOAD_APIK" http://api.imgur.com/2/upload.xml | grep -o http[^\<]* | grep png`
echo $LONGURL
fi

if [ "$UPLOADMETHOD" = "s3" ]; then
S3PUBLIC=`s3cmd put --acl-public --guess-mime-type $FILENAME s3://$DOMAIN/$BASENAME | grep -o http[^\<]*`
if [ "$S3_CUSTOMURL" = "false" ]; then
LONGURL=$S3PUBLIC
else
LONGURL=$LONGURL
fi
fi





# Generate short URL (if configured) and copy to clipboard
if [ "$URLMETHOD" = "long" ]; then
echo "$LONGURL" | xsel --clipboard --input
fi

if [ "$URLMETHOD" = "tinyurl" ]; then
TINYURL_GEN="http://tinyurl.com/api-create.php?url=$LONGURL"
TINYURL_URL=`curl $TINYURL_GEN`
echo "$TINYURL_URL" | xsel --clipboard --input
fi

if [ "$URLMETHOD" = "isgd" ]; then
ISGD_GEN="http://is.gd/api.php?longurl=$LONGURL"
ISGD_URL=`curl $ISGD_GEN`
echo "$ISGD_URL" | xsel --clipboard --input
fi

if [ "$URLMETHOD" = "bitly" ]; then
BITLY_GEN="http://api.bit.ly/v3/shorten?login=$URL_APIU&apiKey=$URL_APIK&format=txt&longUrl=$LONGURL"
BITLY_URL=`curl $BITLY_GEN`
echo "$BITLY_URL" | xsel --clipboard --input
fi





# Delete the local file
if [ "$DELETELOCAL" = "true" ]; then
rm $LOCALDIR/$BASENAME
fi


#2 User is offline   Mike Mansell 

  • Member
  • Pip
  • Group: Members
  • Posts: 14
  • Joined: 31-May 10

Posted 06 March 2011 - 11:40 PM

#!/bin/bash

# Screenshot and Upload Script
# Requires gawk curl xsel scrot openssl imagemagick sshpass

# This script uses OpenSSL to generate MD5 hases for filenames.
# These hashes are based on the file itself and not the timestamp.
# When all is said and done, this prevents users from systematically downloading your screenshots/uploads.

########################
# Upload Configuration #
########################

# "s3", "cloudfiles", "ssh", "ftp", "imageshack", "imgur"
# Note for SSH users: if you have keys configured, leave the "login" and "pass" values blank.
UPLOADMETHOD=""

# Only used for SSH and FTP
SERVER=""

# S3 API key, CloudFiles user, FTP User, or SSH user when using sshpass
LOGIN=""

# S3 API secret, CloudFiles API key, Imgur API Key, FTP password, or SSH pass when using sshpass
PASS=""

# Container, S3 bucket, or server directory (not needed for Imageshack/Imgur)
CONTAINER=""

#####################
# URL Configuration #
#####################

# "long", "bitly", "isgd", or "tinyurl"
# CloudApp users must configure this option in their account
URLMETHOD=""

# API user for URL service (currently only needed for bit.ly)
URL_APIU=""

# API key for URL service (again, currently only needed for bit.ly)
URL_APIK=""

# The base URL that your uploads are stored at (not needed for Imageshack/Imgur)
# THIS IS NEEDED EVEN IF YOU ARE USING A URL SHORTNER
BASEURL=""

#######################
# Misc. Configuration #
#######################

# true/false - Apply a dropshadow to screenshot?  Does *not* apply to file uploads.
DROPSHADOW=""

# true/false - Delete the local SCREENSHOT after upload?  Doesn't apply to file uploads.
DELETELOCAL=""

# true/flase - Keep a local upload ledger?
USELOG=""


###################################################
###### START THE SCREENSHOT AND UPLOAD PROCESS ####
###################################################

SCRIPTDIR=`dirname "$0"`

DATE=`date +%Y%m%d%H%M%S`

if [ "$1" = "" ]; then
	ACTION="screenshot"
	FILE_FULL="$SCRIPTDIR/$DATE.png"
	scrot -s $FILE_FULL
	if [ "$DROPSHADOW" = "true" ]; then
		convert $FILE_FULL -gravity northwest -background 'rgba(255,255,255,0)' -splice 10x10 \
		\( +clone -background gray -shadow 80x12-1-1 \) +swap -background none -mosaic +repage \
		\( +clone -background gray -shadow 80x12+5+5 \) +swap -background none -mosaic +repage $FILE_FULL
	fi
	else
		ACTION="upload"
		FILE_FULL="$1"
		if [ ! -f $FILE_FULL ]; then
			zenity --error --text "\"$FILE_FULL\" does not exist.  Script cannot continue."
			exit
		fi
fi
FILE="`openssl < $FILE_FULL md5`.`echo $FILE_FULL | awk -F . '{print $NF}'`"
MIMETYPE="`file --mime-type $FILE_FULL | cut -d " " -f2`"
CLASSIFICATION=`echo $MIMETYPE | cut -d "/" -f1`

if [ "$UPLOADMETHOD" = "cloudapp" ]; then
	RESPONSE=`curl -G --digest -u $LOGIN:$PASS -H "Content-Type: application/json" -H "Accept: application/json" http://my.cl.ly/items/new | sed 's/${filename}//' | sed 's/\"params\"://' | sed 's/{//g' | sed 's/}//g' | sed 's/\":/\"=/' | sed 's/\":\"/\"=\"/g'`
	function gets3values {
		s3step=$1
		if [ "$s3step" = "" ]; then
			s3step=1
		fi
		item=`echo $RESPONSE | cut -d "," -f$s3step | sed 's/\"//g'`
		if [ "$item" != "" ]; then
			eval $item
			gets3values $(expr $s3step + 1)
		fi
	}
	gets3values
	S3RESPONSE=`curl -sS -D - -o /dev/null -F "key=$key$FILE" -F "acl=$acl" -F "Policy=$policy" -F "AWSAccessKeyId=$AWSAccessKeyId" -F "Signature=$signature" -F "success_action_redirect=$success_action_redirect" -F "file=@$FILE_FULL" "$url"`
	LOCATION=`echo -n $S3RESPONSE | grep -o http[^\<]* | cut -d " " -f1 | cut -d "&" -f1,2`
	CARESPONSE=`curl -G --digest -u $LOGIN:$PASS -H "Accept: application/json" "$LOCATION" | sed 's/:null/=\"null\"/g' | sed 's/:true/=\"true\"/g' | sed 's/{//g' | sed 's/}//g' | sed 's/\":/\"=/' |sed 's/\":\"/\"=\"/g'`
	function getcavalues {
		castep=$1
		if [ "$castep" = "" ]; then
			castep=1
		fi
		item=`echo $CARESPONSE | cut -d "," -f$castep | sed 's/\"//g'`
		if [ "$item" != "" ]; then
			eval $item
			getcavalues $(expr $castep + 1)
		fi
	}
	getcavalues
	LONGURL=$url
elif [ "$UPLOADMETHOD" = "s3" ]; then
	RFCDATE="`date -R`"
	POLFILE="$SCRIPTDIR/POLFILE$DATE.tmp"
	echo { >> $POLFILE
	echo   \"expiration\": \"2015-06-15T12:00:00.000Z\", >> $POLFILE
	echo   \"conditions\": [ >> $POLFILE
	echo	 {\"acl\": \"public-read\"}, >> $POLFILE
	echo     {\"bucket\": \"$CONTAINER\" }, >> $POLFILE
	echo     [\"eq\", \"\$key\", \"$FILE\"], >> $POLFILE
	echo     [\"eq\", \"\$Content-Type\", \"$MIMETYPE\"], >> $POLFILE
	echo	 [\"eq\", \"\$x-amz-storage-class\", \"REDUCED_REDUNDANCY\"] >> $POLFILE
	echo   ] >> $POLFILE
	echo } >> $POLFILE
	POLICY="`base64 -w0 $POLFILE`"
	SIGNATURE="`base64 -w0 $POLFILE | openssl dgst -sha1 -hmac $PASS -binary | openssl base64`"
	curl "key=$FILE" -F "acl=public-read" -F "Policy=$POLICY" -F "AWSAccessKeyId=$LOGIN" -F "Signature=$SIGNATURE" -F "Content-Type=$MIMETYPE" -F "x-amz-storage-class=REDUCED_REDUNDANCY" -F "file=@$FILE_FULL" http://$CONTAINER.s3.amazonaws.com
	LONGURL="$BASEURL/$FILE"
	rm $POLFILE
elif [ "$UPLOADMETHOD" = "cloudfiles" ]; then
	eval $(curl -s -X "GET" -D - \
	  -H "X-Auth-Key:$PASS" \
	  -H "X-Auth-User:$LOGIN" \
	  https://api.mosso.com/auth | gawk '$1=="X-Storage-Token:" { sub(/\r/,"",$2);printf("TOKEN=\"%s\"\n",$2); } 
	                                    $1=="X-Storage-Url:" { sub(/\r/,"",$2);printf("SURL=\"%s\"\n",$2) }
	                                    $1=="X-CDN-Management-Url:" { sub(/\r/,"",$2);printf("CDN=\"%s\"\n",$2) }')
	curl -s -X "PUT" -T "$FILE_FULL"  \
	     -H "X-Auth-Token: $TOKEN" \
	     -H "Content-Type: $MIMETYPE" \
	     $SURL/$CONTAINER/$FILE
	LONGURL="$BASEURL/$FILE"
elif [ "$UPLOADMETHOD" = "ssh" ]; then
	if [ "$LOGIN" = "" ] && [ "$PASS" = "" ]; then
		scp -P 22 $FILE_FULL $SERVER:$CONTAINER/$FILE
	else
		RESULT=`sshpass -p $PASS scp -P 22 $FILE_FULL $LOGIN@$SERVER:$CONTAINER/$FILE`
		echo $RESULT
	fi
	LONGURL="$BASEURL/$FILE"
elif [ "$UPLOADMETHOD" = "ftp" ]; then
	DIRNAME="`dirname $FILE_FULL`"
	BASENAME="`basename $FILE_FULL`"
	cd $DIRNAME
	ftp -n -v $SERVER <<- DONEWITHFTP
		user $LOGIN $PASS
		cd $CONTAINER
		put $BASENAME $FILE
		bye
	DONEWITHFTP
	LONGURL="$BASEURL/$FILE"
elif [ "$UPLOADMETHOD" = "imageshack" ]; then
	if [ "$CLASSIFICATION" = "image" ]; then
		OLDFILE="$FILE_FULL"
		FILE_FULL="`dirname $OLDFILE`/$FILE"
		mv $OLDFILE $FILE_FULL
		LONGURL=`curl -H Expect: -F fileupload="@$FILE_FULL" -F xml=yes -# "http://www.imageshack.us/index.php" | grep image_link | grep -o  http[^\<]*`
		mv $FILE_FULL $OLDFILE
		FILE_FULL="$OLDFILE"
	else
		zenity --error --text "Imageshack is an image hosting service.  You are trying to upload a \"$CLASSIFICATION\" file.  This simply cannot be done."
		exit
	fi
elif [ "$UPLOADMETHOD" = "imgur" ]; then
	if [ "$CLASSIFICATION" = "image" ]; then
		OLDFILE="$FILE_FULL"
		FILE_FULL="`dirname $OLDFILE`/$FILE"
		mv $OLDFILE $FILE_FULL
		LONGURL=`curl -s -F "image=@$FILE_FULL" -F "key=$PASS" http://imgur.com/api/upload.xml | grep -E -o "<original_image>(.)*</original_image>" | grep -E -o "http://i.imgur.com/[^<]*"`
		mv $FILE_FULL $OLDFILE
		FILE_FULL="$OLDFILE"
	else
		zenity --error --text "Imgur is an image hosting service.  You are trying to upload a \"$CLASSIFICATION\" file.  This simply cannot be done."
		exit
	fi
else
	zenity --error --text "This script is not configured to upload to \"$UPLOADMETHOD\".  Please select a valid option and try again."
	exit
fi

if [ "$URLMETHOD" = "long" ]; then
	URL="$LONGURL"
elif [ "$URLMETHOD" = "bitly" ]; then
	URL=`curl "http://api.bit.ly/v3/shorten?login=$URL_APIU&apiKey=$URL_APIK&format=txt&longUrl=$LONGURL"`
elif [ "$URLMETHOD" = "isgd" ]; then
	URL=`curl "http://is.gd/api.php?longurl=$LONGURL"`
elif [ "$URLMETHOD" = "vgd" ]; then
	URL=`curl "http://v.gd/create.php?format=simple&url=$LONGURL"`
elif [ "$URLMETHOD" = "tinyurl" ]; then
	URL=`curl "http://tinyurl.com/api-create.php?url=$LONGURL"`
else
	zenity --error --text "A valid URL method was not specified so no URL was copied to your clipboard."
fi
echo -n "$URL" | xsel --clipboard --input

if [ "$DELETELOCAL" = "true" ] && [ "$ACTION" = "screenshot" ]; then
	rm $FILE_FULL
else
	DELETELOCAL="false"
fi

if [ "$USELOG" = "true" ]; then
	LOG="$SCRIPTDIR/log.csv"
	if [ ! -f $LOG ]; then
		echo "unix_time,friendly_time,action,local_file,mime_type,service,container,item,url,deleted" >> $LOG
	fi
	echo "$DATE,`date +%Y-%m-%d_%H-%M-%S`,$ACTION,$FILE_FULL,$MIMETYPE,$UPLOADMETHOD,$CONTAINER,$FILE,$URL,$DELETELOCAL" >> $LOG
fi

This post has been edited by Mike Mansell: 06 March 2011 - 11:41 PM


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users