Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Net::FTP - sending

by Anonymous Monk
on Mar 02, 2002 at 20:09 UTC ( [id://148880]=perlquestion: print w/replies, xml ) Need Help??

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

I plan to use the Net::FTP module to transer files between systems. In the process, I also need to send certain commands to the "remote system" before "Putting" the file over. I didn't find any method within the Net::FTP module which supports this. Does anyone have any experience doing something similar, or knows of other modules which support sending "commands" across the FTP connection ? Thanks. KM

Replies are listed 'Best First'.
Re: Net::FTP - sending
by runrig (Abbot) on Mar 02, 2002 at 20:15 UTC
    FTP stands for "File Transfer Protocol". It doesn't do shell commands. For that you can look at Net::Telnet or (depending on your concerns about security) Net::SSH or Net::SSH::Perl. Of course, if security was a concern, you wouldn't be using FTP anyway, you'd be using SFTP or SCP anyway (for which those have Net::* modules also...)
Re: Net::FTP - sending
by koolade (Pilgrim) on Mar 02, 2002 at 20:30 UTC

    Are you thinking about the SITE command? If so, Net::FTP does have a site() method.

Re: Net::FTP - sending
by jlongino (Parson) on Mar 03, 2002 at 07:51 UTC
    A few options:
    • You might look into the documentation for Net::Cmd module. Documentation from the Net::FTP perldocs is sketchy at best:
      Methods for the adventurous "Net::FTP" inherits from "Net::Cmd" so methods defined in "Net::Cmd" + may be used to send commands to the remote FTP server. quot (CMD [,ARGS]) Send a command, that Net::FTP does not directly support, to the remote server and wait for a response. Returns most significant digit of the response code. WARNING This call should only be used on commands that do not require data connections. Misuse of this method can hang the connection.
      Note the warning. Unfortunately, the Net::Cmd docs don't say how the commands are executed. I suspect (if anyone out there knows for sure, let me know) that it is using rsh to run the commands. This requires that the host you are accessing has rsh enabled, but on many systems it is disabled by sysadmins for security purposes.

    • The second option is to use a combination of Net::FTP and Net::Telnet. I have done this successfully, but it does require that you have both ftp and telnet access to the host. Here is a sample program:
      #!/usr/local/bin/perl use strict; use Net::Telnet (); ### Season to taste my $tn = Net::Telnet->new(Timeout => 15, Prompt => '\>'); my $host = 'not.arealsite.net'; my $login = 'login'; my $passwd = 'passwd'; $tn->open($host); $tn->login($login, $passwd); my $msg = $tn->errmsg; if ($msg) { print "A system error was generated on the login attempt:\n"; print " '$msg'\n\n"; } ### Execute the remote script that creates the file we want to ftp my @list = $tn->cmd("./somescript.pl"); my $msg = $tn->errmsg; if ($msg) { print "An error occurred when executing cmd './somescript.pl':\n"; print " '$msg'\n\n"; } chomp @list; if ($list[0]) { print "*** The following message was return from HOST: '$host'\n"; foreach my $rec (@list) { print "$rec\n"; exit; } } ### close telnet connection my $ok = $tn->close; if (not $ok) { print "\n\n\nUnable to close Telnet connection to HOST: $host\n\n\n" +; exit; } use Net::FTP; my $ftp = Net::FTP->new($host); my $RC = $ftp->login($login, $passwd); if (not $RC) { print "\n\nFTP Login to Remote Host: '$host' failed!\n\n"; print "No files updated from Remote host: '$host'!\n\n"; exit; } ### Delete old local copy of file to be ftp'd my $infile = "target.dat"; if (-e $infile) { unlink $infile; } ### Get new copy of remote file $RC = $ftp->get($infile); if (not $RC) { print "\n\nget command for remote file: '$infile' failed!\n"; print "No files updated from Remote host: '$host'.\n\n"; exit; } ### close ftp connection $ftp->quit;

    --Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://148880]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found