1832957 Members
2916 Online
110048 Solutions
New Discussion

Re: FTP

 
SOLVED
Go to solution
Ron Bell
New Member

FTP

I am trying to write a script to ftp/put a file to another server.

script 1
ftp -i my_server
user my_id my_password
cd /target_dir
put my_file
quit

script 2
( echo open my_server
sleep 1
echo my_id
sleep 1
echo my_password
sleep 1
echo "cd /target_dir"
sleep 1
ehco "put my_file"
sleep 1
echo quit
) | ftp

Both of these seem to hang at the ftp login. Then when I ctrl-c, they continue with errors.

Any suggestions?

Thanks, Ron
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: FTP

Hi Ron:

In the past, I did this stuff in the shell but now I tend to use Perl because it it so much cleaner.

When I have to do ftp scripting these days, I use perl with the Net::FTP module (available for download at www.perl.org/CPAN). It really makes FTP operations very simple. Any error conditions and timeouts are easy to test for and set.

It's usually something as simple as this (with error checking omitted):

use Net::FTP;

$ftp = Net::FTP->new("remotehost.name", Debug => 0);
$ftp->login("anonymous",'cstephen@xyz.com');
$ftp->cwd("/downloads");
$ftp->get("Testfile.TXT");
$ftp->quit;


As a bonus this works in the wonderful world of Windows as well.


Regards, Clay
If it ain't broke, I can fix that.
Thierry Poels_1
Honored Contributor
Solution

Re: FTP

Hi,

script 1 will start ftp and then the script will hang until you leave ftp. Then it will try user, cd, put & quit on the command line.

try:

ftp -n my_server << [EOT]
user my_id my_passwd
cd /target
put my_file
bye
[EOT]

Mind to put [EOT] solely on a line, don't add any character or space!

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
James R. Ferguson
Acclaimed Contributor

Re: FTP

Hi Ron:

Here are two ways to do this:

#!/usr/bin/sh
ftp -n << EOF
verbose
open thehost
user uuu ppp
get /tmp/stuff
close
EOF

...

#!/usr/bin/sh
{ echo "open thehost
user uuu ppp
hash
mput *
close"
} | ftp -i -n -v 2>&1 | tee /tmp/ftplog.$$

Regards!

...JRF...
S.K. Chan
Honored Contributor

Re: FTP

Try this ..

To get
======
#!/bin/ksh
[[ $# != 5 ]] && { echo "Usage: $0 "; exit 1; }
{ echo user $1 $2; echo cd $5; echo mget $4; echo quit; } | ftp -i -n $3

To put
======
#!/bin/ksh
[[ $# != 5 ]] && { echo "Usage: $0 "; exit; }
{ echo user $1 $2; echo cd $5; echo mput $4; echo quit; } | ftp -i -n $3
harry d brown jr
Honored Contributor

Re: FTP

Clay,

That is the best solution todate! I love it!

Thanks!

live free or die

harry
Live Free or Die
Ron Bell
New Member

Re: FTP

Thank you Thierry! I just needed a quick fix and you were right on.

Thanks also to everyone else. I will try out some of the other solutions, especially the perl one.

Thanks,
Ron