BASH – upload files to ‘pomf.se’ and ‘sprunge’

bash icon

For people out there just like me, who prefer Linux based distro for their daily use and use Linux as their dektop operating system, this script is essential. Just like in windows, we have tools that will share the files online and provide you with the direct link to download those files (examples include puu.sh, ge.tt and belive me, there are many more), this script does the same but from our beloved command line. Now, as for me, I use urxvt-unicode as my terminal emulator (simply because it’s awsome), I have a tool called ‘scrot’ to take the picture of my working desktop and save it in an image file. To upload that file, I will just run:

scrot -d 4 image.jpg | sh upload.sh image.jpg

and the result will be a direct download link that you can copy/paste to another windows for others to view. Pretty neat stuff 😉

‘ -d ‘ for delay in seconds. There are various switches that you can use with scrot, just head to the man page (man scrot) and you will know what I mean.

Now the twist here is that if you have .txt file that you want to upload, then you will need to head towards another website such as pastebin.com (or a variant) to upload that text file. In this script, I have used ‘sprunge’ which is a command line tool to paste the text to the website directly. So, basically, you input the parameters to the script and it will distinguish between the image files and the text files, uploads them accordingly (either to pomf.se or to sprunge) depending upon the file extension type. I have added various file types as you can see and edit below in the script. If, however, you try to be a smart ass and be like let’s upload a file with no extension, then the script will actually ask you what kind of file it is (image file or text/doc file) and take the necessary actions.

Save the script and name it ‘upload.sh’ (or anything you like) and make it executable (chmod u+x upload.sh).

Usage: upload.sh [file 1]…[file3]…[file6]……[file n]

(ofcourse, without the braces)

Exact example would be:

./upload.sh image.jpg nginx.conf pass.txt image2.PNG

Enough talking, here is the script that I have been babbling about above, feel free to tinker with it and make it according to your choice, afterall this is a free world 🙂 Happy scripting!


#!/bin/bash
#
# Author: Aman Hanjrah
# Date: 31 March, 2014
# License: GPL
# Purpose: This is a simple script that will upload a file (text or image) depending upon the file extension to either 'sprunge' or 'pomf.se' and provide you with the direct link.
#
# Feel free to tinker with it as per your liking. Happy scripting 🙂
#
#
# START
#
DST_URL="http://pomf.se/upload.php"
RTRN_URL="http://a.pomf.se"

if [ $# -lt 1 ]
then
echo "File does not exist."
echo "Usage: `basename $0` [file1] [file2] ... [n]"
echo "Hit 'ENTER' to exit."
echo " "
read
exit 1
fi
function upload() {
if [[ "$i" = *.@(jpg|png|svg|jpeg|gif|bmp|tif|tiff|JPG|PNG|SVG|JPEG|GIF|BMP|TIF|TIFF) ]]
then
echo "Uploading of $i in progress..."
OUTPUT=`curl -sS -F files[]=@"$i" "$DST_URL" | cut -d "," -f5 | cut -d":" -f 2 | tr '"' " " | cut -d" " -f2`
echo "$i: $RTRN_URL/$OUTPUT"
echo " "
elif [[ "$i" = *.@(txt|csv|html|xhtml|php|sh|xml|rtf|conf|sql) ]]
then
echo "Uploading of $i in progress..."
OUTPUT=`cat "$i" | curl -sS -F 'sprunge=<-' http://sprunge.us`
echo "$i: $OUTPUT"
echo " "
else
echo "File is of unknown extension, please let me know should I upload it to 'SPRUNGE' (text files), or 'POMF' (image files). Select one of the numbers below corresponding to the type of the file"
echo "1. Image file"
echo "2. Text file"
read "TYPE"
if (( "$TYPE" == 1 ))
then
echo "Uploading $i in progress..."
OUTPUT1=`curl -sS -F files[]=@"$i" "$DST_URL" | cut -d "," -f5 | cut -d":" -f 2 | tr '"' " " | cut -d" " -f2`
echo "$i: $RTRN_URL/$OUTPUT"
echo " "
elif (( "$TYPE" == 2 ))
then
echo "Uploading $i in progress..."
OUTPUT1=`cat "$i" | curl -F 'sprunge=<-' http://sprunge.us`
echo " "
fi

fi
}

for i in "$@"; do
upload
done
#END

If you have any question or comments or any suggestions that I might include in the script, you are most welcome. Either comment below or shoot me an email at: questions@techlinux.net.

Leave a Reply

Your email address will not be published. Required fields are marked *