Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: SFTP and the Pointy-Haired manager

by TilRMan (Friar)
on Jul 08, 2004 at 02:09 UTC ( [id://372648] : note . print w/replies, xml ) Need Help??

in reply to SFTP and the Pointy-Haired manager

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:

$ perl -v This is perl, v5.6.1 built for i386-linux $ perl -MExpect -le 'print $Expect::VERSION' 1.15 $ strings `which sftp` | grep sftp-client @(#)$OpenBSD: sftp-client.c,v 1.33 2002/06/23 09:30:14 deraadt Exp $

Replies are listed 'Best First'.
Re^2: SFTP and the Pointy-Haired manager
by FatDog (Beadle) on Jul 08, 2004 at 03:41 UTC
    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 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