scripting_ftp

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Subject: FTP that will script please
From: "Jim Baxter" <jbaxter@morsco.com>
Date: Wed, 26 Jan 2000 09:53:14 -0600


Hi

We do not seem to be able to run ftp from a shell script or script ftp.
The man page talks about macros but they go away at close.
What we need is the ability to start ftp, login and get (or put) a list of
files and
log off all from a shell script

Can some one tell me how to do it or where to get RPM of another version of
ftp that
will do it?

===

Subject: Re: FTP that will script please
From: Nico De Ranter <nico@sonycom.com>
Date: Wed, 26 Jan 2000 17:00:33 +0100 (MET)



I used to use the following script to do that:

---------------------------------------
#!/bin/sh

{ 
     echo "bin" ;
     echo "prompt" ;
     echo "cd pub/" ;
     echo "get thefile" ;
     echo "bye";
} | ftp ftp.somewhere.com
--------------------------------------

===

Subject: Re: FTP that will script please
From: Gustav Schaffter <gustav@schaffter.com>
Date: Wed, 26 Jan 2000 17:10:40 +0100


Jim,

Use perl: (I'm sure someone will improve on this script. It's just a
quick hack.)

#!/usr/bin/perl -w
use Net::FTP;
$ftp=Net::FTP->new('ftp-server',Timeout => 30,Debug => 0) or die("Failed
to connect. : $! $? $@\n");
$ftp->login('user-name',,'password') or die("Failed to login. : $!
$?\n");
$ftp->hash() or die("Failed to set hash mode. : $! $?\n");
$ftp->put('my-source-file','target-file') or die("Failed to put file. :
$! $?\n");
$ftp->close() or warn("Failed to close. Oh, well... : $! $?\n");
exit;

===

Subject: RE: FTP that will script please
From: Ward William E PHDN <wardwe@nswcphdn.navy.mil>
Date: Wed, 26 Jan 2000 11:33:56 -0500


Perl has a library that allows that.  I've used it for years, and it's
very reliable except for one problem... the password feature is unencrypted.
You would need to use the require "ftp.pl"; directive in your program to 
use the library, though.  I'll send you a short sample with this, but it's
up to you to fix it more than this.

Bill Ward

Short Sample:

#! /usr/bin/perl
require "ctime.pl";
require "ftp.pl";
require "flush.pl";
use File::Path;

# Prototype Perl FTP example by William Ward, 1/26/2000

$ENVP'PATH'}='/bin:/usr/bin';
$ENV{'SHELL'}='/bin/csh';

$my_name=$0;
$my_pid=$$;
$ftpHost="ftp.redhat.com";
$USER_NAME="anonymous";
$USER_PASSWD="me@mybox.org";
$file="myfile";
$local_file_name="thisfile";
$time_to_live=72;
$retry_interval=3600;

$message=sprintf("%s ----- START: $my_name (PID=$my_pid)
-----\n",&timestamp(time,2));
&printflush(STDOUT,$message); 
printf "FTP host: $ftpHost\n";
printf "Account Name to use: $USER_NAME\n";
printf "Account Password: $USER_PASSWD\n";
flush(STDOUT);
connect;
exit;

sub error {
  my($error_number, $error_message) = @_;

  my($message) = sprintf("%s ##### ERROR: $error_message.\n",
    &timestamp(time, 2));  
  &printflush(STDERR, $message);
  &done($error_number);
}

sub connect {
    $x=0;
    if (&ftp::open($ftpHost,21,0,1) !=1) {
	printf "Could not connect to $ftpHost, trying again later\n";
	while (($x<=$time_to_live) && (&ftp::open($ftpHost,21,0,1)!=1))
	{
	    printf "Unable to connect to $ftpHost, sleeping $retry_interval
seconds\n";
	    flush(STDOUT);
	    sleep $retry_interval;
	    $x=$x+1;
	    printf "Been trying to connect to $ftpHost for %s hours (max %s
days)\n",($x*$retry_interval/3600),($time_to_live*$retry_interval/86400);
	}
	if ($x>$time_to_live)
	{
	    printf "Can't open $ftpHost at all, giving up\n";
	    printf "Exiting with Error Code 10\n";
	    flush(STDOUT);
	    &error(10,"Giving up on connection");
	}
	printf "Finally got a connection to $ftpHost\n";
    }
    printflush (STDOUT, "Connected to $ftpHost\n");
    ##### Log in #####
    if (&ftp::login($USER_NAME,$USER_PASSWD)!=1) {
	printf "Can't login to $ftpHost\n";
	printf "Exiting with Error Code 20\n";
	flush(STDOUT);
	&error(20,"Exiting due to incorrect Password");
    }
    printflush (STDOUT, "Logged in as $USER_NAME\n");
    ##### Set Mode to Binary #####
    if (&ftp::type("I") !=1) {
	printf "Can't change mode to binary\n";
	printf "Exiting with Error Code 30\n";
	flush(STDOUT);
	&error(30,"Exiting due to problem changing mode to Binary");
    }
    printflush (STDOUT,"Changing mode to Binary\n");
    if (&ftp::get($file,$local_file_name)!=1) {
	    printf "Can't get $file\n";
	    printf "Exiting with error code 100\n";
	    flush(STDOUT);
	    &error (100,"Exiting due to inexistant file");;
	}
	&ftp::close;
}

===

Subject: Re: FTP that will script please
From: "Greg W" <redhat_list@mail.com>
Date: Thu, 27 Jan 2000 03:52:51 +1100


As info that was passed to me.........

a .netrc file

or

a package called expect , look on freshmeat...... most liked expect cause
can be used for many things.

===

Subject: Re: FTP that will script please
From: Steven W Orr <steveo@world.std.com>
Date: Wed, 26 Jan 2000 12:28:06 -0500


I'm sort of amused at the suggestions that are coming out of this
question. The correct answer is: (drum roll please!)

expect

expect is a scripting language designed for programming interactive
responces, especially for programs that do not do IO on std{in,out}.

Ftp *does* read from stdin but your more elegant solution will be expect.

One person responded with a perl interface called ftp. Looks ok, but the
expect solution is much cleaner.

===

Subject: Re: FTP that will script please
From: Robert Burton <rburton@literati.com>
Date: Wed, 26 Jan 2000 13:37:19 -0500


I find ncftp great for this, b/c it has a couple command line utils, 
ncftpput, ncftpget, specfically written for this purpose.

www.ncftp.com


===

Subject: Re: FTP that will script please
From: "J. Scott Kasten" <jsk@titan.tetracon-eng.net>
Date: Wed, 26 Jan 2000 14:41:33 -0500


Actually, wget does ftp as well as http.  It is totally command line driven,
and automaticly resumes file transfers where they left off when the conection
gets broken, much like ncftp.

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu