1833760 Members
2144 Online
110063 Solutions
New Discussion

FTP script.

 
Vishal Ranjan
Occasional Advisor

FTP script.

I want to automate ftp of some file to ftp.host.com (say) with user dummyuser & password dummypass.
Files are stored in say /interfaces/sid/send & i want to copy them to target's same directory.

Can anybody help?

Regards,
Vishal
5 REPLIES 5
Bernd Reize
Trusted Contributor

Re: FTP script.

#!/bin/sh
#
ftp -in <open remoteserver.yourcompany.org
user dummyuser dummypass
ascii
cd /interfaces/sid/send
lcd /interfaces/sid/send
mput *
bye
EOF
Steven Schweda
Honored Contributor

Re: FTP script.

Or, something like:

wget 'ftp://dummyuser:dummypass@ftp.host.com//interfaces/sid/send/*'

The wisdom of storing a password in a script
is, as always, open to question.

http://www.gnu.org/software/wget/wget.html
Steven Schweda
Honored Contributor

Re: FTP script.

Oops. I missed the "to". Wget is useful if
you're trying to pull the files in, not push
them out. (There is a "wput" program out
there somewhere to go the other way, but I
don't know much about it.)
A. Clay Stephenson
Acclaimed Contributor

Re: FTP script.

I really cringe when I see someone list a string of ftp commands with nary a thought given to error checking. This is simply unacceptable in a production environment.

Fortunately, it's trivially easy to do full-blown error checking with Perl's Net::FTP module. The attached Perl script will do everything you need and you don't even have to know any Perl. All you have to do is check the status of the command itself and if it is zero then all was well.

For your specific problem:

typeset -i STAT=0
cd /interfaces/sid/send
ftpput.pl -h target -l dummyuser -p dummypass -d /interfaces/sid/send -B -t 3 myfile1 myfile2 myfile3
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "FTP Xfer failed; status ${STAT}" >&2
fi
exit ${STAT}

----------------------------------------
This would ftp to host target, login as dummyuser, cd to /interface/sid/send, set binary mode, allow for up to 3 retries (-t 3), and then put myfile, myfile2, and myfile3. You don't even have to pass the password on the command line because you can use a .netrc file -- just like real FTP.

Invoke as ftpput.pl -u for full usage. There is also a secure FTP version.

If it ain't broke, I can fix that.
Rasheed Tamton
Honored Contributor

Re: FTP script.

The below will give you a verbose output of the ftp session.

# cat /var/tmp/ftpsct
/usr/bin/ftp -v -n > /var/tmp/ftpsct.log 2> /var/tmp/ftpsct.err <open servername.domain
user name password
prompt
cd /ddir
bin
mput file*
ls /tmp
quit
EOF

Replace the username/pw,destination system, dir, etc.

sh /tmp/ftpsct (run the script)
after that you can verify /var/tmp/ftpsct.err & ftpsct.log files.

Regards,
Rasheed Tamton.