Linux Blog

Coppermine Photo gallery Upload Script

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:15 pm on Sunday, May 31, 2009

This week I bring you a script that I helped Kaleb (who has written posts here before) write. Well, I got him started with it, using curl and he rolled with it and finished it up. Here is the script:

#!/bin/bash
# Script to Upload to http://kporter.homelinux.com/~kaleb/cpg14x
# Written by Kaleb Porter May 23 2009
# with help of www.thelinuxblog.com
# email: porterboy55@gmail.com
# if you wish to use this code for something else please give me credit

IMAGE=”$1″
URL=”http://kporter.homelinux.com/~kaleb/cpg14x/upload.php”
DA=`date ‘+%d%b%y-%N’`
# If the user does not specify a file or url
if [ -z "$1" ]; then
echo “You did not give a file to upload”
echo “Takeing a screenshot in 3 Seconds…”
sleep 3
scrot $DA.png
IMAGE=$DA.png
fi
FI=`echo “$IMAGE” | grep ‘^[a-z]*://’`
AL=2
FIUP=`curl -s -F control=phase_1 -F blaa=continue -F file_upload_array[]=@$IMAGE $URL | grep unique_ID | awk -F\” ‘{print $6}’`
URLUP=`curl -s -F control=phase_1 -F blaa=continue -F URI_array[]=$IMAGE $URL | grep unique_ID | awk -F\” ‘{print $6}’`

#Get the title for the image from the user and change all the spaces to “%20″
echo “Enter a title for the image”
read TITLE1
TITLE=`echo $TITLE1 | sed ‘s/ /\%20/g’`

#Get the Description for the image from the user and change the spaces to “%20″
echo “Enter a discription”
read DES1
DES=`echo $DES1 | sed ‘s/ /\%20/g’`

#Get the keywords for the image from the user and change the spaces to “%20″
echo “Enter keywords (separated by spaces)”
read KEY1
KEY=`echo $KEY1 | sed ‘s/ /\%20/g’`

if [ -z "$FI" ]; then

UNIQUE_ID=$FIUP
#echo “Choose the NUMBER value for the album you want”
#curl -s -F control=phase_2 -F unique_ID=$UNIQUE_ID $URL | awk ‘/name=”album”/{disp=1} {if (disp==1) print} /<\/select>/{disp=0}’ | grep ‘value=”[0-9]“‘ | sed ‘s/<option//’ | sed ‘s/<\/option>//’ | sed ‘s/>//’
#read AL
curl -o /dev/null -d “control=phase_2&unique_ID=$UNIQUE_ID&album=$AL&title=$TITLE&caption=$DES&keywords=$KEY&blaa=continue” $URL
exit 0
else

# If the image is from a URL
UNIQUE_ID=$URLUP
#echo “Choose the NUMBER value for the album you want”
#curl -s -F control=phase_2 -F unique_ID=$UNIQUE_ID $URL | awk ‘/name=”album”/{disp=1} {if (disp==1) print} /<\/select>/{disp=0}’ | grep ‘value=”[0-9]“‘ | sed ‘s/<option//’ | sed ‘s/<\/option>//’ | sed ‘s/>//’
#read AL
curl -o /dev/null -d “control=phase_2&unique_ID=$UNIQUE_ID&album=$AL&title=$TITLE&caption=$DES&keywords=$KEY&blaa=continue” $URL
exit 0
fi

If there are any questions you can pretty much read the Shell Script Sundays column and figure out everything you need to know. Now that the upload script works, and tries to take a screenshot with scrot, the next step is a check to see if scrot exists, if it doesn’t a check for import, if not an error message.

It really does amaze me at the capabilities of the shell. Especially how mashable it is and how you can combine it with pretty much anything, this script is a great example of combining the power of the shell with the intrawebs. Well, I hoped you learned something, and as always if you have any questions, you know where the comment box is.

- Owen.

Easy script to upload to an ftp server

Filed under: Shell Script Sundays — Kaleb at 11:43 am on Sunday, February 10, 2008

This weeks Shell Script Sunday article is a guest post by Kaleb. Kaleb has been helping me out a by writing for me since I have a project for The Linux Blog that I’d like to complete and can’t dedicate as much time to writing right now. So if you get a chance visit his site and drop some comments.

Hello it’s Kaleb from http://kpstuff.servebeer.com again and today I have come to tell you about a little shell script that I wrote. It’s a script that allows for extremely easy and simple uploads to an ftp server.

The script is actually quite simple utilizing just standard bash scripting along with ncftp which is just an ftp client with some special features. So you need to install ncftp in order to use this script.

Gentoo:

emerge -av ncftp

Arch Linux:

pacman -Sy ncftp

Ubuntu:

apt-get install ncftp

After you install that you need to edit the upload.sh file to fit your needs. There are a few variables you need to change such as your username and password for the server, and the server itself. It is fairly simple to configure.

Set the user variable to your username

user=”kaleb”

Set the passwd variable to your password

passwd=”kalebspass”

Then you need to set the server variable to your server you want to connect to

server=”kpstuff.servebeer.com”

Be sure to not precede the url with something like http:// or ftp://

Finally you should set the default directory on the server for your file to be placed into.

DIR=”/home/$user”

Use $user for your username

Also if your system is odd you may need to make sure that /usr/bin/ncftpput exists. if it does not you will need to find out where on your system ncftp is and change the variable FTPCOM to suit your system.

Now all you need to do is run “chmod +x upload.sh” or “chmod 755 upload.sh” to make it executable and you are finished.

Usage for this script is quite simple. all you need to do is:

./upload.sh <FILE>

Making sure to replace FILE with the filename of the file you want to upload

And if you want to temporarily send the file to a different directory then you specified in the file.

./upload.sh <FILE> <DIR>

Remember to replace <FILE> with the file you want to upload and replace <DIR> with the directory you want to upload to.

You can obtain the script at this address http://kpstuff.servebeer.com/~admin/scripts/upload.sh . And that is it thank you very much for your time and I hope that this script helps you out and if not at least gives you ideas.