Operating System - HP-UX
1822196 Members
3729 Online
109640 Solutions
New Discussion юеВ

Re: any one has automated shell script to ftp files across the server?

 
vz7r1x
Regular Advisor

any one has automated shell script to ftp files across the server?

I am trying to automate ftp process on a secure server to send files to another server on the same lan but it keeps failing at login/password step. Any idea will be accepted. Here is the sample of what I have:
#!/bin/sh
# FTP Scripts
ftp Dest_Server << LoginId PassWord
lcd /home/LoginId/ftp/
cd /home/LoginId/ftp/
ascii
mput *
quit
EOF
5 REPLIES 5
Jeff_Traigle
Honored Contributor

Re: any one has automated shell script to ftp files across the server?

Aside from the fact that ftp is not secure, having passwords in scripts is really bad practice from a security perspective. Use the .netrc file to restrict visibility of the password to the owner, at least. (Still not great, but a little better anyway.) This will allow ftp to authenticate automatically. See netrc(4) and ftp(1) man pages for details.

Ideally, you should be using sftp with keys.
--
Jeff Traigle
A. Clay Stephenson
Acclaimed Contributor

Re: any one has automated shell script to ftp files across the server?

First, I would give you a severe tongue lashing if you were working for me because there is zero error checking. You can use a local .netrc file to get around the password prompt but a much easier way is to leverage Perl's Net::FTP module so that you get error checking for free. The attached Perl script will do everything for you and you don't need to know any Perl.

Here is all that is required:
#!/usr/bin/sh

cd /home/LoginId/ftp # local directory
ftpput.pl -h Dest_Server -l username -p topsecret -d /home/LoginId/ftp -A *
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Operation Ok"
else
echo "FTP operation failed; status ${STAT}." >&2
fi

Invoke as ftpput.pl -u for full usage --- and you can include a .netrc file so that the password (-p topsecret) argument is not required.

If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: any one has automated shell script to ftp files across the server?

If you don't want to follow the advice of Clay and Jeff, here is the solution:
$ ftp Dest_Server <user LoginId PassWord
lcd /home/LoginId/ftp/
cd /home/LoginId/ftp/
ascii
prompt
mput *
quit
EOF
perumal_2
Frequent Advisor

Re: any one has automated shell script to ftp files across the server?

Hi

One more way to achieve this
#! /bin/sh
USER="xxxx"
PASS="xxxxxx"
cd "directory which holds the files to be ftped"

ftp -n destinationservername <quote user $USER
quote pass $PASS
cd "to the destination directory in the destination server"
mput *
HI

You can even write a for loop if you want to copy only specific types of files to be ftped by using find and also delete the files from source once completed using
!command to delete

use "find" to define the file names and pass it to a variable and use the variable in the for loop
example
a=`find . -name "****" -exec ls {} \;`
for i in $a
do
ftp -n destinationserver <quote user $USER
quote pass $PASS
cd "to the destination directory"
put $i
HI
done

"Happy Scripting"

TQ
Perumal
vz7r1x
Regular Advisor

Re: any one has automated shell script to ftp files across the server?

although I received great suggestions, it did not solve my problem, script still fails at login/password verification. I have more pressing issues right now so I will deal with it later.

Thank you all for help.