Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Nov 1998 14:15:02 -0800 (PST)
From:      Ken McGlothlen <mcglk@serv.net>
To:        Steven P Yang <spyang@MIT.EDU>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: how do I automate ftp? (clarification)
Message-ID:  <199811022215.OAA08215@ralf.serv.net>
References:  <199811021917.OAA13614@scrubbing-bubbles.mit.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
Steven P Yang (spyang@mit.edu) used the evil N-word when he wrote:

| Hi, about my last e-mail, a specific goal I have in mind for an automated ftp
| would be the ability to schedule a file transfer at a specific time to a
| remote ftp server.  So, this implies that the ftp script would automatically
| supply the username and password.  I can do this on NT, but don't know if I
| can do this on FreeBSD.
| 
| Is this possible?

Of *course* it's possible, Steven.  In a wide variety of ways.

Something that would provide a lot of flexibility down the road (if this
application got more complicated), but starts off pretty simple, would be the
following Perl script:

	#!/usr/bin/perl

	use Net::FTP;

	$serv = "bargle.fleen.org";
	$user = "wibble";
	$pass = "narfpoit";

	$ftp = Net::FTP->new( $serv );		# Create the FTP object
	$ftp->login( $user, $pass );		# Log in.
	$ftp->binary();				# Change to binary mode.
	$ftp->cwd( "/pub/wibble" );		# Change directories.
	$ftp->mkdir( "floompf" );		# Create a directory.
	$ftp->put( "farble.txt" );		# Put a file.
	$ftp->get( "elbraf.txt" );		# Get a file.
	$ftp->quit();				# Ends the session.

You could do something similar in tcl or even sh, but I like Perl.

Once your script works, you can schedule it easily enough using the crontab
facility.  crontab allows you to edit a file called (oddly enough) crontab that
looks something like this:

	# DO NOT EDIT THIS FILE - edit the master and reinstall.
	# (/tmp/crontab.9722 installed on Sun Oct 18 22:31:43 1998)
	# (Cron version -- $Id: crontab.c,v 1.6 1996/08/05 00:50:02 pst Exp $)
	SHELL=/bin/sh
	PATH=/etc:/bin:/sbin:/usr/bin
	#
	#mn hr   dy  mo  wd  command
	*   */2  *   *   *   /home/wibble/bin/scriptname

Crontab, like Perl, uses "#" as a comment-to-end-of-line indicator.  This
crontab defines two environment variables, and then sets up the script you just
wrote (here called /home/wibble/bin/scriptname) to run once every two hours.

Note that if you wanted the script to run at midnight on the 1st and the 15th
of every month, that line would read:

	0  0  1,15  *  *  /home/wibble/bin/scriptname

If you take a look at the crontab manpage ("man 5 crontab"), you could find out
more about this.

I daresay that this winds up being a *lot* more flexible than NT's version.

Good luck.  If you have any questions, let me know.

							---Ken

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199811022215.OAA08215>