Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

SFTP and the Pointy-Haired manager

by FatDog (Beadle)
on Jul 07, 2004 at 23:25 UTC ( [id://372606]=perlquestion: print w/replies, xml ) Need Help??

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

We have a product manager who wants us to push a report file to a customer every week. We told him we could do it with "FTP" to either our internal FTP server or to a FTP service at the customers site.

He went out and with great effort got the IT guys at the customer site to provide us with ... a SFTP account.

("FTP", "SFTP", what's the difference?) ... grrrr

Several months ago my boss (a decent guy) tried to install Net::SFTP and ran into all the dependencies that make it a pain to install. After a lost day of productivity, he does not want to try again.

So I am stuck in the middle.

I pray for guidence. How can I get SFTP to work from a Perl script under Linux WITHOUT installing Net::SFTP?

Yes, if I setup a trust-key relationship it would be a simple system call to run "sftp file_name name@server:" - but there is no way the customer would do that.

(Their IT guys had a fit because we put the date into the report file-name and they prefer to limit the account to 1 or 2 fixed file names. - Paranoid but I respect them for that.)

Someone suggested I use Expect::Simple as a wrapper around the sftp progam to feed in the password. Has anyone done this?

Replies are listed 'Best First'.
Re: SFTP and the Pointy-Haired manager
by TilRMan (Friar) on Jul 08, 2004 at 02:09 UTC

    I've never used Expect::Simple, but here it is in plain Expect:

    #!/usr/bin/perl use strict; use warnings; use Expect; my $REMOTE = 'name@server:/dir'; my $LOCAL = 'file'; my $cmd = "sftp $REMOTE"; my $exp = Expect->spawn($cmd) or die; $exp->expect(120, "password: ") or die; $exp->send("dummy\r"); $exp->expect(120, "sftp> ") or die; $exp->send("put $LOCAL\r"); $exp->expect(600, "sftp> ") or die; $exp->send("bye\r"); $exp->close();

    It behooves you to add reasonable messages to the die commands, since all Expect scripts inevitably break.

    The code may need tweaking depending on software versions. For the record, here's what I used:

      Cool. I will try to get expect installed tomorrow.

      I did come up with a work around that solves the problem under Linux. It involves creating a command file for SFTP (with the PUT command), then creating a short Expect script that calls SFTP and responds with the password, then .. running the script.

      I will post the code snippet tomorrow as I am home right now.
        Here is the cleaned-up routine I wrote that seems to work under Linux:
        #--------------------------------------------------------------------- +---------------------------------------- # Routine: DoSFTPCommands # Description: This routine will take an array of FTP commands and ex +ecute them using SFTP. # # Input: # aCmdList - Array of FTP commands like: # "chdir /data/reports" # "rm weekly_rpt_*.csv" # "put /new/reports/weekly_rpt_2004_07_01.csv" #--------------------------------------------------------------------- +---------------------------------------- sub DoSFTPCommands { my (@aCmdList) = @_; # Put the FTP commands into a file so we can pass the file name to + the SFTP program open (CMD_FILE, ">ftp.cmd"); foreach (@aCmdList) { print CMD_FILE "$_\n"; } close CMD_FILE; # Create a Expect script to run SFTP my $lCmd = '#!/usr/bin/expect spawn sftp -b ftp.cmd MyAccount@ftp.myCompany.com expect -exact " password: " send "myPassword"; interact '; open (EXP_FILE, ">auto.exp"); print EXP_FILE "$lCmd"; close EXP_FILE; # Make the script runable system ("chmod +x auto.exp"); # Run the script system ("./auto.exp"); # Clean up by deleting the two files we created unlink ("ftp.cmd"); unlink ("auto.exp"); } # DoSFTPCommands
Re: SFTP and the Pointy-Haired manager
by tstock (Curate) on Jul 08, 2004 at 00:34 UTC
    Are you sure you are dealing with SFTP and not FTPS ? There is a difference and maybe Net::SFTP is just not the right module for the task at hand. In any event, I solved my issue using a tool called lftp and suplying the password as a command line argument. It can use either protocol.

    Tiago
Re: SFTP and the Pointy-Haired manager
by thor (Priest) on Jul 08, 2004 at 11:22 UTC
    How were you trying to install Net::SFTP? If you were trying to install the modules by hand, I can imagine that it was a pain. I tend to let the CPAN shell do all that work, though. perl -MCPAN -eshell is my friend.

    thor

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found