如何在不覆盖 TTY 的情况下将密码传递给 su/sudo/ssh?
我正在编写一个 C Shell 程序,它将执行 su
或 sudo
或 ssh
。 他们都希望在控制台输入(TTY)而不是标准输入或命令行中获得密码。
有人知道解决办法吗?
设置无密码 sudo
不是一个选项。
expect 可能是一个选项,但它不存在于我的精简系统。
I'm writing a C Shell program that will be doing su
or sudo
or ssh
. They all want their passwords in console input (the TTY) rather than stdin or the command line.
Does anybody know a solution?
Setting up password-less sudo
is not an option.
expect could be an option, but it's not present on my stripped-down system.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
我编写了一些 Applescript,它通过对话框提示输入密码,然后构建自定义 bash 命令,如下所示:
我不确定这是否有帮助。
如果 sudo 接受预加密的密码,那就太好了,这样我就可以在脚本中对其进行加密,而不必担心回显明文密码。 然而,这对我和我的情况有用。
I wrote some Applescript which prompts for a password via a dialog box and then builds a custom bash command, like this:
I'm not sure if this helps.
It'd be nice if sudo accepted a pre-encrypted password, so I could encrypt it within my script and not worry about echoing clear text passwords around. However this works for me and my situation.
对于
ssh
,您可以使用sshpass
:sshpass -p yourpassphrase ssh user@host
。你只需要先下载 sshpass :)
For
ssh
you can usesshpass
:sshpass -p yourpassphrase ssh user@host
.You just need to download sshpass first :)
对于 sudo 你也可以这样做:
For sudo you can do this too:
也许您可以使用
expect
命令?:该命令自动给出密码。
Maybe you can use an
expect
command?:That command gives the password automatically.
我有:
对我有用。
I've got:
Works for me.
此问题的通常解决方案是设置一个帮助程序应用程序来执行需要超级用户访问权限的任务:
http://en.wikipedia.org/wiki/Setuid
Sudo 不适合离线使用。
稍后编辑:SSH 可以与私钥-公钥身份验证一起使用。 如果私钥没有密码,则可以使用 ssh,而不会提示输入密码。
The usual solution to this problem is setuiding a helper app that performs the task requiring superuser access:
http://en.wikipedia.org/wiki/Setuid
Sudo is not meant to be used offline.
Later edit: SSH can be used with private-public key authentication. If the private key does not have a passphrase, ssh can be used without prompting for a password.
这可以通过在要连接的目标主机上设置公钥/私钥来完成。
第一步是为在本地主机上运行脚本的用户生成 ssh 密钥,方法是执行:
然后输入空白密码。 之后,将您的 ssh 密钥复制到您要连接的目标主机上。
注册 ssh 密钥后,您将能够从本地主机执行静默
ssh remote_user@other_host
。This can be done by setting up public/private keys on the target hosts you will be connecting to.
The first step would be to generate an ssh key for the user running the script on the local host, by executing:
Then enter a blank password. After that, copy your ssh key onto the target host which you will be connecting to.
After registering the ssh keys, you would be able to perform a silent
ssh remote_user@other_host
from you local host.当没有更好的选择(如其他人建议的)时, man socat 可以提供帮助:
所有 pty、setsid、ctty 复杂性都是必要的,虽然您可能不需要睡眠那么长时间,但您需要睡眠。 echo=0 选项也值得一看,就像在 ssh 命令行上传递远程命令一样。
When there's no better choice (as suggested by others), then man socat can help:
All of the pty,setsid,ctty complexity is necessary and, while you might not need to sleep as long, you will need to sleep. The echo=0 option is worth a look too, as is passing the remote command on ssh's command line.
看一下
expect
Linux 实用程序。它允许您根据标准输入上的简单模式匹配将输出发送到标准输入输出。
Take a look at
expect
linux utility.It allows you to send output to stdio based on simple pattern matching on stdin.
这是有效的。
This is working.
将 SSH 设置为公钥身份验证,密钥上没有密码。 网上有大量指南。 届时您将不需要密码即可登录。 然后,您可以根据客户端主机名限制密钥的连接。 提供合理的安全性,非常适合自动登录。
Set SSH up for Public Key Authentication, with no pasphrase on the Key. Loads of guides on the net. You won't need a password to login then. You can then limit connections for a key based on client hostname. Provides reasonable security and is great for automated logins.
更好的
sshpass
替代方案是:passh
https://github.com/clarkwang/passh
登录远程服务器
在远程服务器上运行命令
其他传递密码的方法
此处< /a> 我解释了为什么它比 sshpass 和其他解决方案更好。
a better
sshpass
alternative is:passh
https://github.com/clarkwang/passh
Login to a remote server
Run a command on remote server
other methods to pass the password
here I explained why it is better than sshpass, and other solutions.
如下所示:
您还可以传递各种参数, 回声 y | sudo -S pacman -Syu
(虽然这是一个坏主意,但这只是一个例子)
You can also pass various parameters as follows:
echo password | echo y | sudo -S pacman -Syu
(Although that's a bad idea, it's just an example)
我有同样的问题。 用于在远程电脑上创建目录的对话框脚本。
与 ssh 的对话很容易。 我使用 sshpass (之前安装的)。
问候
来自德国泰特斯的
I had the same problem. dialog script to create directory on remote pc.
dialog with ssh is easy. I use sshpass (previously installed).
greetings from germany
titus
基于 @Jahid 的答案,这对我在 macOS 10.13 上有效:
Building on @Jahid's answer, this worked for me on macOS 10.13:
我曾经有一个用例,我需要在同一个命令中运行 Sudo 和 ssh,而无需
stdin
指定所需的所有变量。这是我使用的命令
将该命令分成几部分!
这将允许您使用超级用户通过您的计算机运行命令:
这将允许您传递 ssh 密码并在外部计算机上执行命令:
I once had a use case where I needed to run Sudo and ssh in the same command without
stdin
specifying all the variables needed.This is the command I used
Breaking that command into pieces!
This will allow you to run commands through your machine using Superuser:
This will allow you to pass ssh password and execute commands on external machines:
一种方法是使用 read -s 选项..这样密码字符就不会回显到屏幕上。 我为一些用例编写了一个小脚本,您可以在我的博客中看到它:
http://www.datauniv.com/博客/2013/02/21/a-quick-little-expect-script/
One way would be to use read -s option .. this way the password characters are not echoed back to the screen. I wrote a small script for some use cases and you can see it in my blog:
http://www.datauniv.com/blogs/2013/02/21/a-quick-little-expect-script/
使用:
示例:
希望有帮助..
USE:
Example:
Hope It Helps..
您可以提供密码作为期望脚本的参数。
You can provide password as parameter to expect script.
希望对您有所帮助。
Hope it is helpful.
对于 sudo,有一个 -S 选项用于接受来自标准输入的密码。 这是 man 条目:
这将允许您运行如下命令:
至于 ssh,我已经做了很多尝试来自动化/脚本化它的使用,但没有成功。 似乎没有任何内置方法可以在没有提示的情况下将密码传递到命令中。 正如其他人提到的,“expect”实用程序似乎旨在解决这一困境,但最终,设置正确的私钥授权是尝试自动化此操作时的正确方法。
For sudo there is a -S option for accepting the password from standard input. Here is the man entry:
This will allow you to run a command like:
As for ssh, I have made many attempts to automate/script it's usage with no success. There doesn't seem to be any build-in way to pass the password into the command without prompting. As others have mentioned, the "expect" utility seems like it is aimed at addressing this dilemma but ultimately, setting up the correct private-key authorization is the correct way to go when attempting to automate this.