FTP automation script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FTP automation script
# 1  
Old 10-20-2008
Question FTP automation script

Hi,
I have got a requirement like this.

a parameterized function custFtp which will take 5 i/ps and will do the following tasks.

p1) server name
p2) username
p3) password
p4) path name of the server where the file resides
p5) file name pattern

the function will work like this.
custFtp <servername> <username> <password> <pathname> <filename>

The basic flow is given like this.
(custFtp)
|
| login
|---------|
success | | fail
| |
(checkfile) (sendmailtousers)
|
|
|
|-------------|
| |
file exist | |No files
| (sendmailtousers)
|
(checkfilesize)
|----------------|
| |
match | |does not match
next process (checkfile)

Can anyone please help me out in this regard.I have goen through the existing posts.But none of them has this kind of compact scripting.

Please refer to the attached figure to get the process.

Please can anyone help me out in this regard.
Thanks.
Anirban Datta
FTP automation script-flowjpg

Last edited by ani_datta; 10-20-2008 at 07:43 AM.. Reason: modification...
# 2  
Old 10-23-2008
MySQL tired of having no reply from u guys...........

hey have built up the script myself..............
parameterized......
well help taken from Advanced Bash-Scripting Guide

the script i am pasting below.....

-----------------------------------------------------------------------
#!/bin/sh
#######################################################################
# Script to perform batch ftp. Essentially converts a list of
# of command line arguments into input to ftp.
#
# -h specifies the remote host (default prep.ai.mit.edu)
# -d specifies the remote directory to cd to - you can provide a sequence
# of -d options - they will be cd'ed to in turn. If the paths are relative,
# make sure you get the sequence right. Be careful with relative paths -
# there are far too many symlinks nowadays.
# (default is the ftp login directory)
# -v turns on the verbose option of ftp, and shows all responses from the
# ftp server.
# -f remotefile[:localfile] gets the remote file into localfile
# -m pattern does an mget with the specified pattern. Remember to quote
# shell characters.
# -c does a local cd to the specified directory
# -u specifies the user
# -p specifies the password
# For example,
# ftpget -h expo.lcs.mit.edu -d contrib -u iuser -p ipassword -f xplaces.shar:xplaces.sh \
# -d ../pub/R3/fixes -c ~/fixes -m 'fix*'
# will get xplaces.shar from ~ftp/contrib on expo.lcs.mit.edu, and put it
# in xplaces.sh in the current working directory, and get all fixes from
# ~ftp/pub/R3/fixes and put them in the ~/fixes directory.
# Obviously, the sequence of the options is important, since the equivalent
# commands are executed by ftp in corresponding order
#
###########################################################################
#

E_BADARGS=65
TMPFILE=/tmp/ftp.`date "+%Y%m%d%H%M%S"`
LOGFILE=ftp.`date "+%Y%m%d%H%M%S"`
# ==> Creates temp file, using process id of script ($$)
# ==> to construct filename.
SITE=$2
# ==> May rewrite this to parameterize this for general use.
usage="Usage: $0 [-h remotehost] [-d remotedirectory]... \
[-f remfile:localfile]... [-c localdirectory] [-m filepattern] [-v]"
echo "user ${USER-gnu} loging into site ${SITE} > ${TMPFILE}"
echo "user ${USER-gnu} loging into site ${SITE} > ${LOGFILE}"
ftpflags="-i -n"
verbflag=
set -f # So we can use globbing in -m
set x `getopt vh:uSmilie:d:c:m:f: $*`
if [ $? != 0 ]; then
echo $usage
exit $E_BADARGS
fi
shift
trap 'rm -f ${TMPFILE} ; exit' 0 1 2 3 15
# ==> Signals: HUP INT (Ctl-C) QUIT TERM
# ==> Delete tempfile in case of abnormal exit from script.
for i in $* # ==> Parse command line args.
do
case $i in
-v) verbflag=-v; shift;;
-h) echo open $2 >> ${TMPFILE}; shift 2;;
-u) remuser=$2; shift 2;;
-p) echo user ${remuser} $2 >> ${TMPFILE}; echo binary >> ${TMPFILE}; shift 2;;
-d) echo cd $2 >> ${TMPFILE};
if [ x${verbflag} != x ]; then
echo pwd >> ${TMPFILE};
fi;
echo "ls -l /tmp/filesize.log" >> ${TMPFILE}
shift 2;;
-c) echo lcd $2 >> ${TMPFILE}; shift 2;;
-m) echo mget "$2" >> ${TMPFILE}; shift 2;;
-f) f1=`expr "$2" : "\([^:]*\).*"`; f2=`expr "$2" : "[^:]*:\(.*\)"`;
echo get ${f1} ${f2} >> ${TMPFILE}; shift 2;;
--) shift; break;;
esac
# ==> 'lcd' and 'mget' are ftp commands. See "man ftp" . . .
done
if [ $# -ne 0 ]; then
echo $usage
exit $E_BADARGS
# ==> Changed from "exit 2" to conform with style standard.
fi
if [ x${verbflag} != x ]; then
ftpflags="${ftpflags} -v"
fi
if [ x${remhost} = x ]; then
remhost=10.244.187.29
# ==> Change to match appropriate ftp site.
fi
echo quit >> ${TMPFILE}
# ==> All commands saved in tempfile.
ftp ${ftpflags} < ${TMPFILE} >> ${LOGFILE}
# ==> Now, tempfile batch processed by ftp.
# Now checking the ftp status
if [ $? -ne 0 ];then
echo "Error in the FTP file moving. on `date` " >> ${LOGFILE}

cnt=`grep -c "Transfer complete" ${LOGFILE}`

if [ $cnt -eq 0 ];then
cnt1=`grep -c "logged in" ${LOGFILE}`

if [ $cnt1 -eq 0 ];then
echo "Invalid username or password" >> ${LOGFILE}
else
echo "FTP failed" >> ${LOGFILE}
fi
fi

exit 0
else
echo "The FTP file is moved successfully.." >> ${LOGFILE}
fi

rm -f ${TMPFILE}
# ==> Finally, tempfile deleted (you may wish to copy it to a logfile).
-----------------------------------------------------
usage:
sh ftpget1.ksh -h <server ip> -u <username> -p <password> -c <local directory> -d <remote dir> -f <remote file>:<loacl file> -v

--------------------------------------------------------

hopefully u can get help from here...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script help needed to manage FTP automation

Hi, I am very new in shell scripting. Have managed to prepare a script which will work to download data to local directory. But facing below problem. :wall: Please help me out. Thanks in advance. Problem: 1. I am unable to use delete command to delete data in the ftp location (I have... (2 Replies)
Discussion started by: ImranBD
2 Replies

2. UNIX for Dummies Questions & Answers

Having a hell of atime with KSH -ftp automation

Following this thread: https://www.unix.com/ip-networking/1935-automated-ftp-task.html I have created the following script: #! /bin/ksh HOST=ftp.mywebsite2.com USER=astrocloud PASSWD=8**** exec 4>&1 ftp -nv >&4 2>&4 |& print -p open $HOST print -p user $USER $PASSWD print -p cd... (3 Replies)
Discussion started by: Astrocloud
3 Replies

3. Shell Programming and Scripting

FTP automation with special characters in userid

Hi, i am trying to automate an ftp script which is as below.But my user id has special characters(aaa\$ifg). So it is not working correctly.Can anyone help on this?I tried providing both of them in double & singe quoted. But somehow it is not picking the "\". Also tried keeping \ before the... (3 Replies)
Discussion started by: aeroticman
3 Replies

4. Shell Programming and Scripting

How to track exit status of ftp automation

ftp automation code is ftp -v -n -i $host_name << EOF user $u_name $u_pass bi mput $tar_file bye EOF How to check whether the file is successfully transfered or not. Suppose the user name or password is provided wrongly then the code should track the error and ask the end user to enter... (2 Replies)
Discussion started by: Dip
2 Replies

5. UNIX for Advanced & Expert Users

FTP automation Job scheduling

Hi, I am working in Unix and Teradata fastload. I need to automate file transfer through ftp from windows path to Unix directory at a specific time, then I should call fastload scripts execution. I have got the fastload script. Entire process should be automated without any manual intervention. It... (1 Reply)
Discussion started by: SATYAPRIYA_D
1 Replies

6. Cybersecurity

FTP Automation Windows <> Unix <> Remote

Hi All, I am a newbie to unix and scripting. I need to do the following job: 1. Create a batch file in windows that will call a script in a remote unix box. 2. The script now ftp files from the Remote windows machine and get them back to the local windows. Actually, I have written the script... (3 Replies)
Discussion started by: Ankur
3 Replies

7. Filesystems, Disks and Memory

Urgent FTP script automation

Hi guys, Here is my requirement for ftp script that i have to automate in unix using shell script: 1) Find the files that atre created one week from the present day. 2) ftp them to the backup server. 3) At the end of the month make a new directory on my backup server with the new month(eg:Once... (1 Reply)
Discussion started by: koduri0475
1 Replies

8. UNIX Desktop Questions & Answers

Urgent FTP script automation

Hi guys, Here is my requirement for ftp script that i have to automate in unix using shell script: 1) Find the files that atre created one week from the present day. 2) ftp them to the backup server. 3) At the end of the month make a new directory on my backup server with the new month(eg:Once... (1 Reply)
Discussion started by: koduri0475
1 Replies

9. UNIX for Dummies Questions & Answers

Urgent FTP script automation

Hi guys, Here is my requirement for ftp script that i have to automate in unix using shell script: 1) Find the files that atre created one week from the present day. 2) ftp them to the backup server. 3) At the end of the month make a new directory on my backup server with the new month(eg:Once... (1 Reply)
Discussion started by: koduri0475
1 Replies

10. UNIX for Dummies Questions & Answers

Automation of telnet and ftp

I have a basic query. I use telnet and ftp very frequently. I want to do it without spending time in typing username and password everytime. I know that if I have .netrc file which contains server address, username, pasword, then just typing ftp will conect to that server with that username and... (10 Replies)
Discussion started by: asutoshch
10 Replies
Login or Register to Ask a Question