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
#!/bin/sh
expect << 'EOS'
spawn sftp USERNAME@ip_address:/path/
expect "Password:"
send "PASSWORD\n"
expect "sftp>"
send "put file1\n"
expect "sftp>"
send "bye\n"
EOS
If you are familiar with perl, I would use that with Expect.
Or, use rsync over ssh.
Or, use rsync over ssh.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
I'm with Jan, way easier to use rsync than sftp + many times sftp is throttled + ssh/rsync runs full speed.
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]:/inco ming/edira pid <<'EOS'
[password]
put /apps/dev/data/transfer/[s tuff]/*.*
bye
EOS
#!/bin/sh
sftp [userid]@[destination.url.
[password]
put /apps/dev/data/transfer/[s
bye
EOS
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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/
PASSWORD
put file1
bye
EOS
or Alternatively:
scp file1 USERNAME@ip_address:/path/
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.