1

I would like to automate a bash script, that connects to server using sftp and does a file transfer. I have the password for this, and initially I tried for this

sftp $acc@$host << EOF
<passwd_here>
cd $dir
get $file
quit
EOF

but it still prompted for password, and I had to enter it manually at the prompt.

After searching SO, I found this post which had a solution with expect, which I tried and I got the following error:

Script:

sftp -b cmdfile.txt $acc@$host
expect "Password:"
send "<passwd>\n";
interact

Error:

Permission denied (publickey,keyboard-interactive).

cmdfile.txt

cd $dir
get $file
quit

Please let me know, How to connect using the password in a bash script?

Community
  • 1
  • 1
mtk
  • 13,221
  • 16
  • 72
  • 112
  • 2
    Better `expect scripts` can be written but I would suggest getting sshpass on your client host. sshpass is more secured and less error prone than sftp. – anubhava Sep 20 '12 at 11:31

3 Answers3

5

Please try the below steps

lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--
jmlemetayer
  • 4,774
  • 1
  • 32
  • 46
cmdilip
  • 99
  • 1
  • 3
  • 1
    I'd never heard of `lftp` before; this is a very useful solution for those of us who don't have the option of setting up SSH keys. Thanks. – ccbunney Dec 03 '15 at 15:10
-2

With scp/sftp you should use key-based authentication. Public key from the user you want to authenticate copy into ~/.ssh/authorized_keys file on the server, into home directory of user on which you want log on. Storing password in clear text on client side is not a good practice, you know :) That way you "workaround" problem of reading password from the prompt too.

Piotr Wadas
  • 1,838
  • 1
  • 10
  • 13
  • 1
    The remote host has provided the password. I can't set the public keys on the host. Please let me know the way to pass it in the script. – mtk Sep 20 '12 at 08:00
  • What do you mean by "remote host provided the password" ? If it'd have provide password for logging-in user, logging would not make sense at all. If you're able to use scp/sftp, then you're probably able to copy public key on remote server, to remote user's ~/.ssh/authorized_keys ( or "authorized_keys2" .. generate your keys with ssh-keygen.. – Piotr Wadas Sep 20 '12 at 08:08
-2

Yes key-based auth is the way to go. Check here for some direction.

opaque
  • 394
  • 1
  • 10