如何在不覆盖 TTY 的情况下将密码传递给 su/sudo/ssh? [英] How to pass the password to su/sudo/ssh without overriding the TTY?

查看:28
本文介绍了如何在不覆盖 TTY 的情况下将密码传递给 su/sudo/ssh?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 C Shell 程序,它将执行 susudossh.他们都希望在控制台输入(TTY)而不是标准输入或命令行中输入密码.

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.

有人知道解决办法吗?

设置无密码sudo 不是一种选择.

Setting up password-less sudo is not an option.

可能是一个选项,但它是不存在于我的精简系统中.

expect could be an option, but it's not present on my stripped-down system.

推荐答案

对于 sudo,有一个 -S 选项用于接受来自标准输入的密码.这是 man 条目:

For sudo there is a -S option for accepting the password from standard input. Here is the man entry:

    -S          The -S (stdin) option causes sudo to read the password from
                the standard input instead of the terminal device.

这将允许您运行如下命令:

This will allow you to run a command like:

echo myPassword | sudo -S ls /tmp

至于 ssh,我曾多次尝试将其使用自动化/编写脚本,但均未成功.似乎没有任何内置方式可以在没有提示的情况下将密码传递给命令.正如其他人所提到的,expect" 实用程序似乎旨在解决这一难题,但最终, 尝试自动执行此操作时,设置正确的私钥授权是正确的方法.

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.

这篇关于如何在不覆盖 TTY 的情况下将密码传递给 su/sudo/ssh?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆