Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I want to get a remote file (that is not on the server the .pl file is) and copy it on the server where the .pl file is. Can this be done with Perl? And can you post a short example? Thank you!

Replies are listed 'Best First'.
Re: Getting remote files
by Joost (Canon) on Sep 03, 2002 at 15:46 UTC
    I'm not really sure this is what you mean, but you can use LWP for fetching files that can be reached with an URL or Net::SSH if you need to get it with SSH.

    A short example with LWP::Simple :

    use LWP::Simple; my $document = get("http://some.site/filename.extention");
    Read the docs for LWP for much more info :-)
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Getting remote files
by defyance (Curate) on Sep 03, 2002 at 15:47 UTC
    Are you able to access this file remotely, like via FTP?
    If so, you may want to check out the documentation for Net::FTP

    An Example(UNTESTED):

    #!/usr/bin/perl -w use strict; use Net::FTP; my $file="filename"; my $rdir="/path/to/remote/dir"; my $ftp = Net::FTP->new("yourhost.com", Debug => 0); $ftp->login("username",'-username@'); $ftp->cwd("$rdir"); $ftp->get("$file"); $ftp->quit;
    Then, you can either move the file manually, or if your wanting to keep it as automated as possible add something like this:

    my $ldir="/path/to/local/script"; system("mv", "$rdir$/$file", "$ldir");

    I also know of some more friendly File Manipulation modules, but have drawn a blank on the names..

    --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--
    perl -e '$a="3567"; $b=hex($a); printf("%2X\n",$a);'

Re: Getting remote files
by Preceptor (Deacon) on Sep 03, 2002 at 16:18 UTC
    scp host:/path/to/file . rcp host:/path/to/file . lynx -dump http://url > file

    I think one of the modules I installed added 'GET' and 'POST' aliases in /usr/local/bin which allow interacting with a HTTP server.
    At the end of the day though, it is not possible unless there's _some_ form of server app on the remote host.
    What it is will depend primarily on what platform/resources you have available.
Re: Getting remote files
by simeon2000 (Monk) on Sep 03, 2002 at 15:42 UTC
    This is not possible without having something set up on the server to handle the request. You COULD conceivably write a TCP server on REMOTE_HOST and have it respond to request from a perl script on CLIENT_HOST... but it would probably just be easier to install Secure Shell on both of them and have perl utilize the Secure Copy program.

    Although perl can do everything, sometimes its best to use the toolbox approach. If somebody has already made a good screwdriver, you don't have to forge your own.

    --
    perl -e "print qq/just another perl hacker who doesn't grok japh\n/"
    simeon2000|http://holdren.net/

Re: Getting remote files
by Anonymous Monk on Sep 03, 2002 at 17:51 UTC
    Thanks to all of you!!! I found this
    http://search.cpan.org/src/NWIGER/File-Remote-1.14/
    But don't know how to install it and what I will need...
      The easiest way to install it is outlined in the Tutorials section, under A Guide to Installing Modules

      However, here is a quick and easy way to do it..
      From the command line:

      bash$ perl -MCPAN -e shell
      If you have already used CPAN.pm before you will get a command prompt where you type in install File::Remote Otherwise it will take you through an interactive setup, which allows you to choose a whole bunch of different mirrors to get the packages from, nifty! See below for more visual instruction:
      cpan>install File::Remote
      If you're going to install it with the files you found on cpan.org, then follow the instructions in the above mentioned Tutorial.

      Infact, just ignore what I wrote and go view that tutorial, as I'll probably only confuse you!

      P.S. I forgot to mention, it will find all dependencies for you, as far as modules are concerned!

      --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--
      perl -e '$a="3567"; $b=hex($a); printf("%2X\n",$a);'