Automating sftp with Tcl

George Peter Staplin: sftp (ssh FTP) has a wonderful feature that allows for executing a script. The script below automates the procedure of uploading files from a local public_html to a network.

To make it easier you can use ssh public keys, so that you don't have to type the password repeatedly. I suggest that you generate a public key (~/.ssh/id_dsa.pub), and upload it to the server's ~/.ssh/authorized_keys, so that you don't need a password.

The authentication agent can help make it easier to manage key passwords.

 $ crontab -l
 @reboot ssh-agent -s | grep -v echo > $HOME/.ssh-agent

Then I just type ssh-add after I have started my box, and enter the private key password.


 #!/usr/bin/env tclsh8.4

 proc main {argc argv} {
  if {$argc < 1} {
   puts stderr "syntax is: [info script] file"
   return 1
  }

  set dir [file normalize .]
  set remote_dir [string range $dir [string first public_html $dir] end]

  puts REMOTE:$remote_dir

  set fd [open _script_ w]
 
  # Prefix the mkdir with - to avoid termination if it already exists.
  puts $fd "-mkdir -p $remote_dir"
  puts $fd "cd $remote_dir"

  foreach f $argv {
   puts "PUT $f"
   puts $fd "put $f\nls -l $f"
  }
 
  puts $fd quit

  close $fd

  if {[catch {exec sftp -b _script_ [email protected]} r] && $::errorCode ne "NONE"} {
   puts stderr ERRORCODE:$::errorCode
   puts stderr ERROR:$r
   return 1
  }
  puts $r 

  file delete _script_

  return 0
 }
 exit [main $::argc $::argv]

Usage:

 $ pwd 
 /home/gps/archive/public_html/tmp
 $ pubup foo.jpg

At this point foo.jpg should be uploaded to the server's public_html/tmp.


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();