How to make ssh receive the password from stdin

二次信任 提交于 2019-11-26 14:41:24

You can't with most SSH clients. You can work around it with by using SSH API's, like Paramiko for Python. Be careful not to overrule all security policies.

based on this post you can do:

Create a command which open a ssh session using SSH_ASKPASS (seek SSH_ASKPASS on man ssh)

$ cat > ssh_session <<EOF
export SSH_ASKPASS="/path/to/script_returning_pass"
setsid ssh "your_user"@"your_host"
EOF

NOTE: To avoid ssh to try to ask on tty we use setsid

Create a script which returns your password (note echo "echo)

$ echo "echo your_ssh_password" > /path/to/script_returning_pass

Make them executable

$ chmod +x ssh_session
$ chmod +x /path/to/script_returning_pass

try it

$ ./ssh_session

Keep in mind that ssh stands for secure shell, and if you store your user, host and password in plain text files you are misleading the tool an creating a possible security gap

You can use sshpass which is for example in the offical debian repositories. Example:

$ apt-get install sshpass
$ sshpass -p 'password' ssh username@server
pier

An old post reviving...

I found this one while looking for a solution to the exact same problem, I found something and I hope someone will one day find it useful:

  1. Install ssh-askpass program (apt-get, yum ...)
  2. Set the SSH_ASKPASS variable (export SSH_ASKPASS=/usr/bin/ssh-askpass)
  3. From a terminal open a new ssh connection without an undefined TERMINAL variable (setsid ssh user@host)

This looks simple enough to be secure but did not check yet (just using in a local secure context).

Here we are.

FreeBSD mailing list recommends the expect library.

If you need a programmatic ssh login, you really ought to be using public key logins, however -- obviously there are a lot fewer security holes this way as compared to using an external library to pass a password through stdin.

starfry

Distilling this answer leaves a simple and generic script:

#!/bin/bash
[[ $1 =~ password: ]] && cat || SSH_ASKPASS="$0" DISPLAY=nothing:0 exec setsid "$@"

Save it as pass, do a chmod +x pass and then use it like this:

$ echo mypass | pass ssh user@host ...

If its first argument contains password: then it passes its input to its output (cat) otherwise it launches whatver was presented after setting itself as the SSH_ASKPASS program.

When ssh encounters both SSH_ASKPASS AND DISPLAY set, it will launch the program referred to by SSH_ASKPASS, passing it the prompt user@host's password:

I'm not sure the reason you need this functionality but it seems you can get this behavior with ssh-keygen.

It allows you to login to a server without using a password by having a private RSA key on your computer and a public RSA key on the server.

http://www.linuxproblem.org/art_9.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!