Home arrow Other Articles arrow Automating SFTP using expect

Main Menu
 Linux Articles
 FreeBSD Articles
 Apache Articles
 Perl Articles
 Other Articles
 Program Downloads
 Free Books
 The Web Links
 Contact Us

Most Read
Automating SFTP using expect
SnortShorwall - Using Snort And Shorewall Together
FreeBSD automatic updates
Shorewall Router on Linux

Favorite Linux/BSD

Latest news direct to your desktop

Login Form


Remember me
Forgotten your password?

Members Online
 Linux-BSD-Central Has a Total of 46451 Members   Members (46451) # Online
 We have 7 Guests Online. Guests 7
 We have 0 Users Online. Users 0

Online Users
No Users Online

OS: Linux j
PHP: 5.2.17
MySQL: 5.5.42-37.1-log
Time: 05:11
Members: 46451
Hits: 2924984
News: 281
WebLinks: 15

Automating SFTP using expect   PDF  Print  E-mail 
Contributed by Chad Brandt  
Saturday, 19 June 2004

You may need to automate transferring of files and the only protocol you have available to you is SSH. You can accomplish this using expect and SFTP

Expect ( is a tool for automating interactive programs. To use expect to automate file transfer with SFTP, we have to first create the expect script


spawn  sftp  -b cmdFile
expect "password:"
send "shhh!\n";

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

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

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

Another alternative would be to use Perl to automate the SCP. You could do this with the following example

#!/usr/bin/perl -w

use Net::SFTP;
use strict;

my $host = "";
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");

Written by Guest on 2004-08-18 10:54:25
Nice ...BUT ???
Written by Guest on 2004-10-07 14:22:05
I have followed the examples here, but still cannot get the password to enter...tried expect as noted above and the full string returned to the script and neither works...any ideas ?? 
Written by Guest on 2004-10-08 07:41:30
did you include the new line character \n at the end of the password
sftp/expect exception conditions
Written by Guest on 2004-10-20 12:56:02
I have a working expect script for sftp and it works fine unless there is an unexpected response or error condition. I have not been able to determine how to handle the many exception conditions that may occur. Is there a good source for this type of information?
Written by Guest on 2004-11-28 14:43:28
:) it works fine!
I don't get password prompt
Written by Guest on 2005-04-15 20:50:39
When I run sftp with -b option it does not seem to allow interactive password entry at all so expect cannot help me. Is there a workaround for this to force it to prompt for the password when in batch mode?
Written by Guest on 2005-07-20 12:15:32
unix shell script for sftp...
Written by Guest on 2005-08-26 02:20:56
Its working fine in linux...but when i tried to execute in unix..I am unable to find "send,spawn,expect" utility files... Is there any other alternative to suppress interactive password during the execution of script? me out in  
unix shell script...
Install expect on unix
Written by Guest on 2005-08-26 07:10:31
expect is not installed by default. Download the program and install it on the unix machine
Written by Guest on 2008-12-30 00:44:09
I believe its not working
expect does not tranfer entire file
Written by Guest on 2009-03-04 13:42:40
expect script does not tranfer entire file ... has anyone come accross this situation
file not found
Written by Guest on 2009-03-07 01:42:40
What if file is not found on the file server and we get a "File not found" message. how can we handle this situation? :?
Written by Guest on 2009-09-28 17:37:14
thanks for this tip - I was beginning to get frustrated because I could not find an easy PHP way to retrieve the files I needed. This in a cron job does miracles !
Nice - thanks.
Written by Guest on 2009-11-23 18:43:48
Could not get perl version working due to missing Math::pari (which was too complicated for me to figure out with my limited perl experience). 
expect however was already installed on my servers (AIX) and worked well.  
Later versions of sftp don't do password prompting when -b flag is given, hence the reason I was searching for a solution. Instead, get rid of the batch file and send the commands via expect/send instead. 
spawn sftp user@host 
expect "password:" 
send "nottelling\n"; 
expect "sftp> " 
send "lcd /xxx \n"; 
expect "sftp> " 
send "cd /yyy \n"; 
expect "sftp> " 
send "ls\n"; 
expect "sftp> " 
send "mget *\n"; 
expect "sftp> " 
send "quit\n"; 
send_user "\ndone.\n"; 
Thank you
Written by Guest on 2010-02-18 19:16:02
The new connection with the below does not work and perl does not complain. 
However, stuffing into %args as described above worked fine. Thanks 
my $sftp = Net::SFTP->new($sftpHost, $sftpUser, $sftpPass ) or print "Cannot connect to $sftpHost: $@"; 
A Question
Written by Guest on 2010-03-26 10:32:37
If we execute this script simultaneously by two users in the same server, error is coming. what to do
Written by Guest on 2010-03-26 10:33:55
can i get a solution
works now!
Written by Guest on 2010-04-28 04:58:44
Cheers guys - very useful info. 
I got an issue where my batch file wouldn't get processed if called from a scheduled job although running from command prompt did the trick! chucking the batch file route and using individual commands working like a treat!
haldle the retrun codes
Written by Guest on 2010-05-20 04:09:03
in sftp how to handle the error codes
Not able to install supprt modules.. plz
Written by Guest on 2010-07-06 09:49:54
SFTP pbm is keep on asking me to install some modules.. like crypt. but was not installing properly. plz let me know what are the support modules I need to hav for SFTP and their links if possible 
Thank you 
Works well
Written by Guest on 2010-07-27 08:07:41
Tried it and it seems to work well, thanks! Found this article from Google
Thanks Chad
Written by Guest on 2010-07-28 09:11:58
Wow after months of on and off searching, your expect script works beautifully!! I was trying lftp etc.. and couldn't get it to work. Thanks man!

Only registered users can write comments.
Please login or register.

Powered by AkoComment 1.0 beta 2!



Check out TwistByte - The best mobile apps available For awesome Android and IPhone applications!!