Shell script for FTP

Sharky Forums


Results 1 to 9 of 9

Thread: Shell script for FTP

  1. #1
    Reef Shark moog's Avatar
    Join Date
    Sep 2000
    Location
    Bath, UK
    Posts
    422

    Question Shell script for FTP

    I wrote a small shell script to automate an ftp transfer as follows:

    #!/bin/sh
    echo "user anonymous
    [email protected]
    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
    [email protected]
    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.
    No Norm, those are your safety glasses... I'll wear my own thanks.

  2. #2
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    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

    ------------------
    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 or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  3. #3
    Hammerhead Shark The_Hitman's Avatar
    Join Date
    Apr 2001
    Location
    Louisville, Kentucky USA
    Posts
    1,318

    Post

    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


    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 a proud member of the SF Linux Hippy Club.

  4. #4
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    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 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 or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  5. #5
    Reef Shark moog's Avatar
    Join Date
    Sep 2000
    Location
    Bath, UK
    Posts
    422

    Post

    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 )

    moog

    ------------------
    No Norm, those are your safety glasses... I'll wear my own thanks.
    No Norm, those are your safety glasses... I'll wear my own thanks.

  6. #6
    Reef Shark moog's Avatar
    Join Date
    Sep 2000
    Location
    Bath, UK
    Posts
    422

    Post

    namgor

    yep - it's from the cpan archive. You can download libnet from 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",'[email protected]');
    $ftp->type("bin");
    $ftp->get("somefile.zip");
    $ftp->quit;

    ------------------
    No Norm, those are your safety glasses... I'll wear my own thanks.
    No Norm, those are your safety glasses... I'll wear my own thanks.

  7. #7
    Sleeps with the Fishes
    Join Date
    Jan 2001
    Location
    Pittsburgh (please click here to donate $5 to city budget)
    Posts
    1,246

    Post

    I was told lftp does retries. Get it from http://lftp.yar.ru/ .

    ------------------
    html.dataraq.com

  8. #8
    Reef Shark moog's Avatar
    Join Date
    Sep 2000
    Location
    Bath, UK
    Posts
    422

    Post

    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.
    No Norm, those are your safety glasses... I'll wear my own thanks.

  9. #9
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    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 or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •