Link to home
Start Free TrialLog in
Avatar of Dan Packer
Dan Packer

asked on

Shell SFTP script with password

Can someone help me out?  I need a shell script that can make an sftp connection with a password - installing the keys is not an option.  The scripts that I have (pasted below) throw an error saying that the command "expect" is not available.  Any help would be GREATLY appreciated.

#!/bin/sh

expect << 'EOS'
spawn sftp USERNAME@ip_address:/path/to/folder
expect "Password:"
send "PASSWORD\n"
expect "sftp>"
send "put file1\n"
expect "sftp>"
send "bye\n"
EOS
Avatar of noci
noci

Whell the whole point in setting up sftp (as part of ssh) in the first place was to get rid of sending passwords....
And use a set of keys in stead....

Expect is not a standard command, it belongs to the tcltk family of software which can built from these sources:
http://expect.nist.gov/
Or otherwise by installing expect on yout system according to your package managers rules. (Again look for Tcl/Tk)..

Now.... why use expect if you can send it directly:
#!/bin/sh

sftp USERNAME@ip_address:/path/to/folder      << 'EOS'
PASSWORD
put file1
bye
EOS

or Alternatively:
scp file1 USERNAME@ip_address:/path/to/folder   <<'EOS'
PASSWORD
EOS

Again the whole purpose of these commands (ssh, scp, sftp) were to get rid of passwords in scripts and have sucure links (non-eaves dropping) between systems.
If you are familiar with perl, I would use that with Expect.  

Or, use rsync over ssh.
ASKER CERTIFIED SOLUTION
Avatar of David VanZandt
David VanZandt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm with noci, avoid using Expect, unless you have a bunch of interactive prompts to script.

I'm with Jan, way easier to use rsync than sftp + many times sftp is throttled + ssh/rsync runs full speed.
Avatar of Dan Packer

ASKER

Noci and David - Thanks - I didn't realize you could just leave out "expect".  However, the revised script still doesn't quite work - If I run the script from the command prompt, I'm still prompted for a password.  Is there any way to prevent that?  

#!/bin/sh
sftp [userid]@[destination.url.com]:/incoming/edirapid <<'EOS'
[password]
put /apps/dev/data/transfer/[stuff]/*.*
bye
EOS
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A stray thought, one I cannot test at the moment, but memory suggests you still need to deconstruct the initial line. Something like,
>sftp
open <target IP address>
username
password
cd x
and so on
sftp requires at least user@host on the commandline user is optional, then the current username is used.