MYSYSAD.COM

FTP Using a One-Liner and Perl Script

· Shell & text · Networking & mail

A colleague of mine, Mahlon Anderson, wanted to revisit the FTP Using a Shell Script post.

Here is what he had to say...

Here is a creative way to put an FTP command on a single line. Why one line? The short story is I needed to do an ftp in a crontab without calling another script.

Three things to note:

1) I have only tested this with Solaris 8.
2) This works in the one true shell, Bourne shell.
3) If you have a "$" in your password, it might cause you problems.

echo “quote user <user_name>\nquote pass <password>\ncd <dir>\nbin\nlcd <dir>\nput <file>” | ftp –n <host>


Use this in a Perl script. (I didn’t have access to the CPAN FTP module)

# vi myftp.pl

#!/usr/bin/perl

$USER = “esoft”;
$PASSWORD=”myftp125”;
$REMOTE=”esoft”;
$DIR=”/jpg_dir;
$FILE=”esoft.jpg”;
$echo = ‘echo “quote user ‘ . $USER. ‘\nquote pass ‘ . $PASSWORD .’\nbin\nlcd ‘ . $DIR . ‘\nmput $FILE\n”’;
@status = ‘$echo| ftp –nv $REMOTE‘;
print @status;
:wq!

The FTP module is for doing FTP directly with Perl...basically system calls to the C library directly. With the right modules, you can do all that from Perl without having to resort to the back quotes.

Run via CLI

# ./myftp.pl