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 (http://expect.nist.gov/) is a
tool for automating interactive programs. To use expect to automate file
transfer with SFTP, we have to first create the expect script
#!/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
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 = "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"); |
Brillant! Written by Guest on 2004-08-18 10:54:25 thx! | 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? | Super! 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?...help 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 | Nice - thanks. Written by Guest on 2009-11-23 18:43:48 Could not get perl version working due to missing Math: ari (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: $@";
|
Powered by AkoComment 1.0 beta 2! |