Auto FTP to a server

Hi,

I am trying to automate FTP login to a remote server and put a file there. But no matter what i do, It still is aksing me for the login and password although i have it defined in the variable.

Could you tell me what is wrong with this script. I am doing this on SUN server.

Pls assist.

Thanks

All those ftp commands should be within a here-document. Or use the co-process technique.

You should find them in the FAQ section.

Thank you so much VINO as always, you have been very helpful :slight_smile:

3 cheers to you !

One more thing vino,

If i need to read the codes for FTP how can i do that ..for example

I need to validate on code 550 and echo a message that the specified file was not found.

How can we do this.

Thanks once again :slight_smile:

Search the forum with the keywords "ftp result code". You will find a couple of posts which will help you out.

I have automated that FTP script few days back and i think ur requirements are almost same...if ur prob is not yet solved will give u that script

Hi Dhruv,

Please do share it here, Will be handy for every one.

Any ways, here is how i did it :-

Hope this helps some one :slight_smile:

Cheers !!!

Hi,

It sounds great that you found the solution.Actually this script is for multiple file transfer we can ftp single file too..and it support metacharecters in filename like file* will transfer all files starting with file.After completion of script log file will be generated and those file names transferred and could not transferred will be displayed on prompt.

Where is the script ??? :slight_smile:

here i am sharing my script

#script name: mfileftp.sh
#purpose: Script File for Multiple Files FTP Program which support and ? metachars
#Details: This script generates one log file which contains server output and custom output as names of files ftp to server
# and contain warning message which says which files could not transferred
# log file will be created in the source directory with name ftp_log_ddmmyyhhmn.log
#USAGE: sh mfileftp.sh ftpserver username pwwd d/sourcedirectory d/destinationdir file1...file2...file

# use d as command line parameter for default otherwise put directory
#prerequisite: user must have write permission in the source directory
#Author:
#version: 1.6

v_source_ip=$1
v_user_id=$2
v_pwd=$3
v_source_dir=/usr/sdir #put some directory for default usage
v_dest_dir=/usr/ddir #put some directory for default usage
#this temp file will remain in existance to run commands on ftp prompt, will be deleted at the end of script.

temp=$v_source_dir/temp_ftp.tmp
underscore_char=_

TIMESTAMP=`date +%d%m%y%H%M`
log=$v_source_dir/ftp_log$underscore_char$TIMESTAMP.log

if [ $# -lt 6 ]
then
echo "Improper Usage:$0"
exit
fi

if [ $# -eq 5 ]
then
echo "improper usage:File name missing"
fi

#Remove temp file if exist

if [ -f $temp ]
then
rm $temp
fi

#Remove log file if exist
if [-f $log ]
then
rm -f $log
fi

#Check if file name is given and validate source and destination directory

if [ $# -gt 5 ]
then
if [ $4 = "d" ]
then
echo " Default Source Dir Used : $v_source_dir"
elif [ -d $4 ]
then
v_source_dir=$4
else
echo "Invalid Source directory or Improper Usage"
exit
fi
# check for destination dir

if [ $5 = "d" ]
then
echo " Default Destination Dir Used : $v_source_dir"
else
v_dest_dir=$5
fi

#Creation of temp file from where all the commands will be executed
#changing mode of file so no one else can see it

touch $temp
chmod 700 $temp
#c=`ls -l $temp`
#echo "$c"
echo "user $v_user_id $v_pwd" > $temp
echo "cd $v_dest_dir" >> $temp
echo "lcd $v_source_dir" >> $temp
echo "binary" >> $temp
#echo "hash" >>$temp

#shift first 5 parameters to get files passed in command line
#in loop we check if file exist then we put in command file else we put in error file change ver 1.6

shift 5
while [ $# -gt 0 ];
do
if [ -f $1 ]
then
echo "mput $1" >> $temp

else
echo "file::\" $1 \" do not exist" >>$v_source_dir/ftp_error.err #this file will not be created if all files passed are valid
fi
shift 1
done
echo "bye" >> $temp

#the following command will open ftp session and and run commands from $temp file
#each command will put its message to the $log file as we have redirected output to the file in place of terminal

ftp -nvi $v_source_ip < $temp >$log

#Now we will append our custom message to the log file which will show the names of files transferred during the run
#with grep command we will take those lines contains filenames and put in temp_fill file
#As filenames are second field in the temp_fill file we cut those fields and append it to $log file

echo "\n\n"
echo "Files Transfered in this run...." >>$log
grep "local:" $log>$v_source_dir/temp_fill
cut -f 2 -d" " $v_source_dir/temp_fill>>$log

if [ -f $v_source_dir/ftp_error.err ] #check if err file contains some info then append it to log file ver 1.6
then
echo "\nWarning:\n">>$log #change ver 1.6
cat $v_source_dir/ftp_error.err>>$log #append error message to log file(change ver 1.6)
fi

#following command will display contents of temp file, name of files FTPed to remote machine(change ver 1.6)
echo "files transfered in this run"
cat $v_source_dir/temp_fill
echo "\n"
if [ -f $v_source_dir/ftp_error.err ]
then
echo "\nWarning:\n"
cat $v_source_dir/ftp_error.err #change ver 1.6
fi

fi #end of outer if which checks for no of parameters are greater than five

#remove temp_fill and temp file for security

rm -f $v_source_dir/ftp_error.err #change ver 1.6
rm -f $v_source_dir/temp_fill
rm -f $temp
echo "For more detailed log information please see log file::$log" #change ver 1.6
echo "Automated FTP script completed..."

thanks a lot for sharing :slight_smile:

try this

#!/bin/sh

if [ "$#" -lt 4 ]
then
echo "Usage: ./ftp_script <M/C Name> <Userid> <Password> <Path>"
exit
fi

ftp -idn << !
open $1
user $2 $3
close
bye
!