Thursday, November 03, 2005

Perl: automate sftp using perl or expect

#!/usr/bin/perl -w

use Net::SFTP;
use strict;

my $host = "host.ssh.com";
my %args = (
user => 'your_user_name,
password => 'your_password',
debug => 'true'
);

my $sftp = Net::SFTP->new($host, %args);
$sftp->get("/home/user/something.txt", "/home/user/hey.txt");
$sftp->put("bar", "baz");




#!/usr/local/bin/expect

spawn sftp -b cmdFile user@yourserver.com
expect "password:"
send "shhh!\n";
interact

This will spawn sftp in batch mode and pass in the password shhh! to the program. SFTP will than execute all the commads in cmdFile

cmdFile
lcd /home/ftp/test
cd /home/ftp/somedir
mput *.dat

lcd /home/recieving
cd /home/someotherdir
mget *.dat


0 Comments:

Post a Comment

<< Home