1833756 Members
2433 Online
110063 Solutions
New Discussion

FTP script

 
SOLVED
Go to solution
John Curtis
Occasional Advisor

FTP script

I want to make an FTP script that can be run automatically to upload files. How do I pass the login name, password and commands from within the script?
4 REPLIES 4
Michael Tully
Honored Contributor

Re: FTP script

One way to do this would be by using netrc
Have a look at the man page on how to do it
man netrc

HTH
Michael
Anyone for a Mutiny ?
Patrick Wallek
Honored Contributor
Solution

Re: FTP script

Another way is to do something like this:

#!/sbin/sh

ftp -i -n -v >> /tmp/logfile 2>&1 << ENDFTP
open machine.domain.com
user username password
bin
cd /remote/dir
put filename
put filename1
bye
ENDFTP

The stuff between the '<
James R. Ferguson
Acclaimed Contributor

Re: FTP script

Hi John:

Using the 'netrc' method has better security implicatations since you don't have to bury a password in a script. However, here's another way:

{
echo "open $HOST
user $WHO $WHOPASS
hash
$ACTION $LOCALFILE $REMOTEFILE
close"
} | ftp -i -n -v 2>&1 | tee -a $LOGFILE

...the $Variables should be self-explanatory. This snipet captures the ftp dialog into $LOGFILE. The "hash" shows the transfer progress.

Regards!

...JRF...
Wodisch
Honored Contributor

Re: FTP script

Hello John,

the archoes must be full of answers for your question,
as I remember having answered quite some of them ;-)

Anyway, create or edit the file $HOME/.netrc and set
permissions to 600 or 400 - it will NOT work otherwise.
The content should be something like:

machine MARS user JOHN password SECRET macdef init
cd /DIR-ON-TARGET
lcd /DIR-ON_LOCAL-STATION
bin
prompt
mget FILE-PATTERN
quit


There must be an empty line after that paragraph.
All the UPPERCASE words must be replaced with your
own values, perhaps in lower-case... ;-)

Which means, you DO have a plain-text password in
that file on your local station! Hence the permissions!

Next time you (the userid with that $HOME directory)
start "ftp" to that target-station, it will NOT ask for
username nor password, but use those from the ".netrc".
Then it will change the directories, remote and locally,
set transfermode to binary, turn off the prompt for Y/N
for each file, and transfer all the files matching that
pattern from remote to local station, and quit.

If you want ASCII-conversion, use "ascii" instead of
"bin", if you want transfer from local to remote use
"mput" instead of "mget" (read: multiple get/put).

HTH,
Wodisch