sftp

SFTP stands for ssh file transfer protocol; curiously, the pertinent Wikipedia article [L1 ] refers to the operative IETF draft [L2 ] only rather obliquely. When encountering the term sftp, one may in fact see references to two different things:

  1. the secure file transfer program client, which is a command line program that comes with OpenSSH and which is an interactive interface similar to the normal ftp clients one may find on their platform.
  2. the true ssh file transfer protocol. http://en.wikipedia.org/wiki/SSH_file_transfer_protocol refers to this as being almost a remote file system protocol. Full support of the protocol is more than just running ftp over SSH-2 or with an SSL layer.

A vfs module for the ssh file transfer protocol would be extremely cool.


While there's a widespread belief that Expect is essentially necessary for effective automation of sftp (first sense), this is not true. Several distinct approaches exist [L3 ] [L4 ] [L5 ] ... [explain] OTOH, most of these seem to be alternative SFTP clients with built-in automation support, so for use from within Tcl the most portable solution may well be to use Expect (or sftp in batch mode, if that is sufficient).


BF - 2012-02-08 16:05:18

This guy claims to have a solution to the FTP on TLS problem https://groups.google.com/group/comp.lang.tcl/browse_thread/thread/2040029cb6d9d626/4a418d988305b0e0?lnk=gst&q=ftp+ssl#4a418d988305b0e0


gavino - 16aug2012

I automated sftp login and change dir and download:
1 without keys
2 SSH was turned OFF on the server, so ONLY pure SFTP connections are not dropped. My Expect attempt failed, Net::SFTP failed since it uses SSH, which was turned off: so I used perl expect in order to script monitor the sftp server's working condition.
I would love to see how tcl or tcl expect would do this without keys, and without ssh: only sftp connections get handled.
code follows:

#!/usr/bin/perl
use strict;
use Expect;

# Uncomment the following line if you want to see what expect is doing
#$Expect::Exp_Internal = 1;

# Uncomment the following line if you don't want to see any output from the script
#$Expect::Log_Stdout = 0;

# set your username/password here
my $sftpUsername = "gerogebush" ;
my $sftpPassword = "texas";
my $sftpServer = "bush.texas.com";
my $fileToFetch1 = "file1";
my $fileToFetch2 = "file2";
my $dir1 = "IN";
my $dir2 = "OUT";
my $timeout = 1;

# If sftp is not in your path replace with absolute path of sftp program
my $command = 'sftp';
my $params = ("$sftpUsername\@$sftpServer");

# Create the Expect object
my $exp = Expect->spawn($command, $params) or die "Cannot spawn sftp command \n";

# If this is the first time you are running this , expect will send "yes" to add the key
# for the sftp server to the ~/.ssh/known_hosts file else
# wait for "Password Authentication" string to show up
$exp->expect($timeout,
        ["Password Authentication"],
        ["Are you sure you want to continue connecting", sub {my $self = shift; $self->send("yes\n");}]
        );

# Wait for Password prompt to show up
#$exp->expect($timeout, ["password:"]);
$exp->expect($timeout, ["password:"]);
#$exp->expect($timeout, ["Password:"]);

# Sent the sftp password
$exp->send("$sftpPassword\n");

# Wait for sftp prompt
$exp->expect($timeout, ["sftp>"]);

$exp->send("cd $dir1\n");

# Wait for sftp prompt
$exp->expect($timeout, ["sftp>"]);

$exp->send("get $fileToFetch1\n");

# Wait for sftp prompt
$exp->expect($timeout, ["sftp>"]);

$exp->send("cd ../$dir2\n");

# Wait for sftp prompt
$exp->expect($timeout, ["sftp>"]);

$exp->send("get $fileToFetch2\n");

# Wait for sftp prompt
$exp->expect($timeout, ["sftp>"]);

# Close ftp session
$exp->send("bye\n");

# Destroy the expect object
$exp->soft_close();