Linux BSD Central SFTP Expect

2
Search Web LinuxBSDCentral.com Home Main Menu Home Linux Articles FreeBSD Articles Apache Articles Perl Articles Other Articles Program Downloads Free Books News The Web Links Contact Us Most Read Automating SFTP using expect FreeBSD PPTP VPN SnortShorwall Using Snort And Shorewall Together FreeBSD automatic updates Shorewall Router on Linux Polls Favorite Linux/BSD Fedora Mandrake Debian Slackware Gentoo Suse FreeBSD Other Vote Results Syndicate Latest news direct to your desktop Login Form Username Password Remember me Login Forgotten your password? Members Online Members (46451) # Online Guests 7 Users 0 Online Users No Users Online Statistics OS: Linux j PHP: 5.2.17 MySQL: 5.5.4237.1log Time: 06:10 Members: 46451 Hits: 2921715 News: 281 WebLinks: 15 Automating SFTP using expect 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 (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 [email protected] 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"); Comments Brillant! Written by Guest on 20040818 10:54:25 thx! Nice ...BUT ??? Written by Guest on 20041007 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 20041008 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 20041020 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 20041128 14:43:28 it works fine! I don't get password prompt Written by Guest on 20050415 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 20050720 12:15:32 unix shell script for sftp... Written by Guest on 20050826 02:20:56 SOFTLAYER® CLOUD A Private Cloud Environment That Is Truly Transparent & Secure.

description

Linux BSD Central SFTP Expect toolunix shell script

Transcript of Linux BSD Central SFTP Expect

Page 1: Linux BSD Central SFTP Expect

Search

Web LinuxBSDCentral.com

Home

Main Menu

Home Linux Articles FreeBSD Articles Apache Articles Perl Articles Other Articles Program Downloads Free Books News The Web Links Contact Us

Most Read

Automating SFTPusing expectFreeBSD PPTP VPNSnortShorwall Using Snort AndShorewall TogetherFreeBSD automaticupdatesShorewall Routeron Linux

Polls

Favorite Linux/BSD

Fedora

Mandrake

Debian

Slackware

Gentoo

Suse

FreeBSD

Other

Vote Results

Syndicate

Latest news direct toyour desktop

Login FormUsername

Password

Remember meLoginForgotten yourpassword?

Members Online

Members(46451)

#Online

Guests 7

Users 0

Online UsersNo Users Online

StatisticsOS: Linux jPHP: 5.2.17MySQL: 5.5.4237.1logTime: 06:10Members: 46451Hits: 2921715News: 281WebLinks: 15

Automating SFTP using expect

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 accomplishthis using expect and SFTP

Expect (http://expect.nist.gov/) is a tool for automating interactive programs. To use expect to automate file transfer withSFTP, we have to first create the expect script

#!/usr/local/bin/expect

spawn sftp b cmdFile [email protected] "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 incmdFile

cmdFilelcd /home/ftp/testcd /home/ftp/somedirmput *.dat

lcd /home/recievingcd /home/someotherdirmget *.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");

Comments

Brillant!Written by Guest on 20040818 10:54:25

thx!

Nice ...BUT ???Written by Guest on 20041007 14:22:05

I have followed the examples here, but still cannot get the password to enter...tried expect as noted above and the fullstring returned to the script and neither works...any ideas ??

Written by Guest on 20041008 07:41:30

did you include the new line character \n at the end of the password

sftp/expect exception conditionsWritten by Guest on 20041020 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 havenot been able to determine how to handle the many exception conditions that may occur. Is there a good source for thistype of information?

Super!Written by Guest on 20041128 14:43:28

it works fine!

I don't get password promptWritten by Guest on 20050415 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 therea workaround for this to force it to prompt for the password when in batch mode?

Written by Guest on 20050720 12:15:32

unix shell script for sftp...Written by Guest on 20050826 02:20:56

S O F T L A Y E R ® C L O U D

A Private Cloud Environment That Is Truly Transparent & Secure.

Page 2: Linux BSD Central SFTP Expect

Its working fine in linux...but when i tried to execute in unix..I am unable to find "send,spawn,expect" utility files... Is thereany other alternative to suppress interactive password during the execution of script?...help me out in unix shell script...

Install expect on unixWritten by Guest on 20050826 07:10:31

expect is not installed by default. Download the program and install it on the unix machine

Written by Guest on 20081230 00:44:09

I believe its not working

expect does not tranfer entire fileWritten by Guest on 20090304 13:42:40

expect script does not tranfer entire file ... has anyone come accross this situation

file not foundWritten by Guest on 20090307 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?

PaulWritten by Guest on 20090928 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 20091123 18:43:48

Could not get perl version working due to missing Math: ari (which was too complicated for me to figure out with mylimited 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 youWritten by Guest on 20100218 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 QuestionWritten by Guest on 20100326 10:32:37

If we execute this script simultaneously by two users in the same server, error is coming. what to do

helloWritten by Guest on 20100326 10:33:55

can i get a solution

works now!Written by Guest on 20100428 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 commandprompt did the trick! chucking the batch file route and using individual commands working like a treat!

haldle the retrun codesWritten by Guest on 20100520 04:09:03

in sftp how to handle the error codes

Not able to install supprt modules.. plzWritten by Guest on 20100706 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 whatare the support modules I need to hav for SFTP and their links if possible Thank you Sri

Works wellWritten by Guest on 20100727 08:07:41

Tried it and it seems to work well, thanks! Found this article from Google

Thanks ChadWritten by Guest on 20100728 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 towork. Thanks man!

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

Powered by AkoComment 1.0 beta 2!