Click to See Complete Forum and Search --> : Shell script for FTP
I wrote a small shell script to automate an ftp transfer as follows:
#!/bin/sh
echo "user anonymous
me@my.com
bin
cd somedirectory
get somefile
bye" | ftp -n ftp.somewhere.com
Works pretty well by scheduling with an at job.
However, I've been attempting to add re-try capabilities to the script with limited success. Here's the new script:
#!/bin/sh
set loopcount = 0
while ($loopcount < 20)
set loopcount = `expr $loopcount + 1`
echo "user anonymous
me@my.com
bin
cd somedirectory
newer somefile
bye" | ftp -n ftp.somewhere.com
sleep 600
end
Here I use the newer command to ensure that if I do get connected again, I don't restart the transfer. Also the sleep command is used to give a 10min break between attempts.
However, I just get a complaint that there's mismatched "." I guess I need to get all the echo stuff on one line, but can't seem to find the correct newline character to put in. (I've tried $'\r' but it doesn't like that). What I really want is "ftp -n somewhere.com -retry 20" but there's no such function...
Can anyone help ?
moog
------------------
No Norm, those are your safety glasses... I'll wear my own thanks.
namgor
06-23-2001, 11:37 AM
Thanks Moog, I was working the same project as you (automate FTP with PERL)... I wonder if we work in the same company. I didnt know the echo ... | ftp works with FTP http://www.sharkyforums.com/ubb/smile.gif
------------------
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
uwcdc.com (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)
The_Hitman
06-23-2001, 11:40 AM
Originally posted by namgor:
Thanks Moog, I was working the same project as you (automate FTP with PERL)... I wonder if we work in the same company. I didnt know the echo ... | ftp works with FTP http://www.sharkyforums.com/ubb/smile.gif
The Net::FTP in perl is absolutly the best way to do this sort of thing... I use it alot for various jobs at work.
------------------
"Given enough time and money..Microsoft will eventually invent a Unix." - ME
Why Yes! I am (http://www.geocities.com/bloodred716/the_hitman.html) a proud member of the SF Linux Hippy Club (http://www.geocities.com/bloodred716/hippy.html).
namgor
06-23-2001, 11:42 AM
Originally posted by The_Hitman:
The Net::FTP in perl is absolutly the best way to do this sort of thing... I use it alot for various jobs at work.
Is Net::FTP some kind of add-ons we need to download from www.perl.com (http://www.perl.com) or how can we cann those functions in Net::FTP ?
------------------
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
uwcdc.com (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)
hitman
Can you give me an example (I've never tried perl) - got anything already written you'd care to share.. ? (Not asking you to spend hours writing something for me!, just if you had something already....)
(I'll buy you a pint as payment if you're in Bath/UK ever http://www.sharkyforums.com/ubb/wink.gif )
moog
------------------
No Norm, those are your safety glasses... I'll wear my own thanks.
namgor
yep - it's from the cpan archive. You can download libnet from www.perl.com/cpan/ (http://www.perl.com/cpan/)
I think you might also be able to do
perl -MCPAN -e shell
install NET::FTP
though I'm not sure.
I'm currently trying to write the script in perl I've got as far as the first script - i.e. downloading a file directly, but am again having trouble with detecting if the login's failed. Here's the script so far if you're interested:
#!/usr/bin/perl
use Net::FTP;
$ftp=Net::FTP->new("ftp.somewhere.com");
$ftp->login("anonymous",'me@my.com');
$ftp->type("bin");
$ftp->get("somefile.zip");
$ftp->quit;
------------------
No Norm, those are your safety glasses... I'll wear my own thanks.
Skeelo
06-25-2001, 10:57 AM
I was told lftp does retries. Get it from http://lftp.yar.ru/ .
------------------
html.dataraq.com (http://html.dataraq.com)
Good shout skeelo!
I've compiled lftp and it does indeed do re-trys.
Here's the script now :
#!/bin/sh
echo "open ftp.somewhere.com
cd somedir
get somefile
bye" | lftp
It then handles the retrys and everything.
Thanks again
moog
------------------
No Norm, those are your safety glasses... I'll wear my own thanks.
namgor
06-28-2001, 03:24 PM
The shell scripts never worked for me, but the perl script rocks! I dont need retry coz server is intrenal.
------------------
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
uwcdc.com (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)