0

I want to input the password to sftp using heredoc:

sftp root@example.com <<EOF
password
EOF

But it does not work. sftp still asks me to type password. I know I can use sshpass to input the password, but why cannot I just use the heredoc to input the password. I think heredoc will be read as the standard input for sftp. Am I misunderstanding something?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
William
  • 761
  • 2
  • 10
  • 27
  • It's not about heredoc. `sftp` does not accept the password using the stdin at all. This won't work either: `echo password|sftp user@example.com`. There are zillion questions here already that cover this. How example: [How to run the sftp command with a password from Bash script?](https://stackoverflow.com/q/5386482/850848) – Martin Prikryl Nov 14 '20 at 09:14

2 Answers2

2

sftp will read the password from /dev/tty or some other program when running under graphical environment. When you run sftp from the terminal, stdin is /dev/tty. The heredoc replaces stdin, but sftp will always read /dev/tty for password.

alef
  • 124
  • 4
1

I suggest to use sshpass:

SSHPASS='password' sshpass -e sftp root@example.com

From man sshpass:

-e: The password is taken from the environment variable "SSHPASS".

Cyrus
  • 84,225
  • 14
  • 89
  • 153