Monday, June 30, 2008

FTP Using a One-Liner and Perl Script

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.



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

6 comments:

Hank Edley said...

Mahlon Anderson from West Central Illinois origins?

esofthub said...

Hank,

I spoke to Mahlon a few hours ago about your comment. He was quite surprised when I mentioned your name. Small world and UNIX makes it smaller.

He wanted me to let you know he's going to respond soon.

Hank Edley said...

Thanks for passing it along .. if your working with Mahlon on a daily basis your working with one of the best.

esofthub said...

You're welcome Hank and I agree with you.

Anonymous said...

Think this guy http://linuxandmicrocontrollertips.blogspot.com/2008/11/ftp-using-one-liner-and-perl-script.html is copying your contents "ditto"

chk out
http://linuxandmicrocontrollertips.blogspot.com/2008/11/ftp-using-one-liner-and-perl-script.html

http://linuxandmicrocontrollertips.blogspot.com/2008/11/ftp-using-shell-script.html

Miral said...

Thanks for sharing ur knowledge