Saturday, February 27, 2010

Shell Script to Upload a File to the Remote FTP Server


#!/bin/bash
# SCRIPT : upload.sh
# USAGE : ./upload.sh [-d remote directory path] -f "File-to-upload"
# -d option is optional by default it takes servers home dir
# or
# ./upload.sh "Filename-to-upload"
#
# PURPOSE : Upload file to remote ftp server.

############### DEFINE FUNCTIONS HERE ##############

Usage()
{
echo " USAGE "
echo " $0 [-d \"Remote Directory Path\"] -f \"Filename-to-upload\""
echo "-------------------------- or -----------------------------"
echo " $0 Filename-to-upload"
exit 1
}

################# ARGUMENTS CHECKING #################

while getopts d:f: choice
do
case $choice in
d) Dname=$OPTARG;;
f) Fname=$OPTARG;;
esac
done

[ $# -eq 1 ] && Fname=$1
[ -z $Dname ] && Dname="/" #change it to suit
[ -z $Fname ] && echo "Bad Arguments" && Usage

[ ! -e $Fname ] && echo "$0: $Fname no such file" && exit 1

############### DEFINE VARIABLES HERE ################

Server="ftp.checksoftware.com" #Remote ftp server.
Username="anonymous" #Remote ftp user name.
Password="venuk@gmail.com" #Remote ftp Password.

# change above to suit

############### MAIN PROGRAM STARTS HERE #############

# Remove comment lines within the here document before run the script

ftp -n $Server <<ServerEnd
user "$Username" "$Password"

# binary command Set the file transfer type to support binary file
# transfer.So you can transfer all type of files.

binary

# hash command is used for printing hash-sign("#") for each data block
# transferred. The size of data block is system dependent.In my box it
# is 1024 bytes. hash command is a toggle switch, by default it is set
# off most systems.

hash # comment it if you don't need it

cd $Dname

# pwd command prints current directory on the remote host. This shows
# your file is uploading to which directory.

pwd # comment it if you don't need it

put "$Fname"
bye
ServerEnd

exit 0

OUTPUT:

[root@www ~]# ./upload.sh
Bad Arguments
USAGE
./upload.sh [-d "Remote Directory Path"] -f "Filename-to-upload"
-------------------------- or -----------------------------
./upload.sh Filename-to-upload
[root@www ~]# ./upload.sh -f samplepic.jpg -d /incoming
Hash mark printing on (1024 bytes/hash mark).
257 "/incoming" is current directory.
###############################################
[root@www ~]# ./upload.sh samplepic.jpg
Hash mark printing on (1024 bytes/hash mark).
257 "/" is current directory.
samplepic.jpg: Permission denied

11 comments:

  1. Before writing original document a person should make an outline for article. He must write down the points in which order he has to proceed with it. He must keep in mind the instructions which have been given by the board for writing an article.personal statement editor

    ReplyDelete
  2. There is in all probability a minimum of one factor on everyone's credit reports that they need sent their version of the incident. client statements ar total waste of your time as a result of most banks don't even examine credit reports any longer. my blog

    ReplyDelete
  3. This post is really amazing I can easily understand the entire concept without spending much time for it. this writing script binary command Set the file transfer type to support binary file. In this blog provide very good articles about script writing. Thank you very much for the sharing. cv pharmacy

    ReplyDelete
  4. Great post!! here Venu k you talk about Shell Script to Upload a File to the Remote FTP Server.This post really helped me to learn so much thing about APD and How do APDs get interviews. This is a very informative blog about higher degree educational program. I you will write more article about this topics with more details.

    ReplyDelete
  5. As soon as you've given on your faculty software, you're probably pretty worried about stepping in to college. Even though you've finished your faculty application, added your senior high school transcripts, your SAT and ACT scores along with your college essay, you're actually probably wondering how exactly will the admissions committee choose a faculty candidate to really become an accepted student? this website makes me happy with the qulity of service.

    ReplyDelete
  6. This article should be shared more often.

    ReplyDelete
  7. The cash is accessible considerably speedier than it is with a bank credit. Unsecured trader loans are extraordinarily an extraordinary alternative for retail and eatery shippers, not just on the grounds that these kinds of organizations can barely be supported by the conventional bank, yet in addition on account of the quick liquidity and straightforward process.The cash is accessible considerably speedier than it is with a bank credit. Unsecured trader loans are extraordinarily an extraordinary alternative for retail and eatery shippers, not just on the grounds that these kinds of organizations can barely be supported by the conventional bank, yet in addition on account of the quick liquidity and straightforward process.

    ReplyDelete
  8. They likewise have highlights of their administrations that give assurance to both the purchaser and the dealer not every single auxiliary merchant make such offers. news about the ticketindustry

    ReplyDelete