mmansell Posted November 26, 2010 Report Share Posted November 26, 2010 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 ([email protected])# 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 formatDROPSHADOW="true" # Use ImageMagick? "true" or "false"######################### Upload Configuration #########################DOMAIN="" # Domain (or S3 bucket) used to upload your grabs toUPLOADMETHOD="" # 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 usernameSERVER_PASS="" # Server passwordSERVER_GDIR="" # Where grabs are stored on the server (SSH or FTP only)UPLOAD_APIK="" # API key to be used with imgur, etcLONGURL="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_keyURL_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 screenshotBASENAME="$DATE.png"FILENAME="$LOCALDIR/$BASENAME"scrot -s $FILENAME# Add drop-shadowif [ "$DROPSHADOW" = "true" ]; thenconvert $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 $FILENAMEfi# Uploadif [ "$UPLOADMETHOD" = "ftp" ]; thencd $LOCALDIRftp -n -v $DOMAIN << DONEWITHFTPuser $SERVER_USER $SERVER_PASScd $SERVER_GDIRput $BASENAMEbyeDONEWITHFTPfiif [ "$UPLOADMETHOD" = "ssh" ]; thensshpass -p $SSH_PASS scp -P 22 $FILENAME $SERVER_USER@$DOMAIN:$SERVER_GDIRfiif [ "$UPLOADMETHOD" = "imageshack" ]; thenLONGURL=`curl -H Expect: -F fileupload="@$FILENAME" -F xml=yes -# "http://www.imageshack.us/index.php" | grep image_link | grep -o http[^\<]*`fiif [ "$UPLOADMETHOD" = "imgur" ]; thenLONGURL=`curl -F "image=@$FILENAME" -F "key=$UPLOAD_APIK" http://api.imgur.com/2/upload.xml | grep -o http[^\<]* | grep png`echo $LONGURLfiif [ "$UPLOADMETHOD" = "s3" ]; thenS3PUBLIC=`s3cmd put --acl-public --guess-mime-type $FILENAME s3://$DOMAIN/$BASENAME | grep -o http[^\<]*`if [ "$S3_CUSTOMURL" = "false" ]; thenLONGURL=$S3PUBLICelseLONGURL=$LONGURLfifi# Generate short URL (if configured) and copy to clipboardif [ "$URLMETHOD" = "long" ]; thenecho "$LONGURL" | xsel --clipboard --inputfiif [ "$URLMETHOD" = "tinyurl" ]; thenTINYURL_GEN="http://tinyurl.com/api-create.php?url=$LONGURL"TINYURL_URL=`curl $TINYURL_GEN`echo "$TINYURL_URL" | xsel --clipboard --inputfiif [ "$URLMETHOD" = "isgd" ]; thenISGD_GEN="http://is.gd/api.php?longurl=$LONGURL"ISGD_URL=`curl $ISGD_GEN`echo "$ISGD_URL" | xsel --clipboard --inputfiif [ "$URLMETHOD" = "bitly" ]; thenBITLY_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 --inputfi# Delete the local fileif [ "$DELETELOCAL" = "true" ]; thenrm $LOCALDIR/$BASENAMEfi Quote Link to post Share on other sites
mikemansell Posted March 7, 2011 Report Share Posted March 7, 2011 (edited) #!/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 FTPSERVER=""# S3 API key, CloudFiles user, FTP User, or SSH user when using sshpassLOGIN=""# S3 API secret, CloudFiles API key, Imgur API Key, FTP password, or SSH pass when using sshpassPASS=""# 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 accountURLMETHOD=""# 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 SHORTNERBASEURL=""######################## 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 fifiFILE="`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=$urlelif [ "$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 $POLFILEelif [ "$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 fielif [ "$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 fielse zenity --error --text "This script is not configured to upload to \"$UPLOADMETHOD\". Please select a valid option and try again." exitfiif [ "$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."fiecho -n "$URL" | xsel --clipboard --inputif [ "$DELETELOCAL" = "true" ] && [ "$ACTION" = "screenshot" ]; then rm $FILE_FULLelse DELETELOCAL="false"fiif [ "$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" >> $LOGfi Edited March 7, 2011 by Mike Mansell Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.