简体   繁体   English

以用户名和密码(和主机)为参数的SSH / rsync / scp

[英]SSH / rsync / scp with username and password (and host) as parameters

I'm figuring out how to best create a bash script that accepts username, password and host as input and can then use ssh/rsync/scp to connect. 我在想如何最好地创建一个bash脚本,该脚本接受用户名,密码和主机作为输入,然后可以使用ssh / rsync / scp进行连接。 It seems that these programs only accept password that is given by the user via prompt. 看来这些程序仅接受用户通过提示输入的密码。

Note: I am well familiar with SSH keys - my use case is a situation where one wants to programmatically place an SSH key to a server where a key does not exist. 注意:我对SSH密钥非常熟悉-我的用例是一种情况,有人希望以编程方式将SSH密钥放置到不存在密钥的服务器上。

My current solution is to use expect to answer the password prompt with the correct password (and provide all other information as parameters). 我当前的解决方案是使用expect用正确的密码回答密码提示(并提供所有其他信息作为参数)。

See https://gist.github.com/elnygren/965a6db4f3fd8e242e90 参见https://gist.github.com/elnygren/965a6db4f3fd8e242e90

如果您不介意密码对其他用户可见,则可以按照此答案中的建议使用sshpass

sshpass -p<password> ssh <arguments>

The best solution for jobs like these (connecting anything over SSH, be it ssh itself, scp or rsync) is to use keys for authentification. 对于此类工作(通过SSH连接任何东西,无论是ssh本身,scp还是rsync),最好的解决方案是使用密钥进行身份验证。

Then you can add your key to the auth manager (or just leave it without passphrase, but then be careful!) and use it to connect to the host. 然后,您可以将密钥添加到身份验证管理器中(或将其保留无密码,但要小心!)并使用它来连接到主机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM