ssh 坚持从控制台读取密码而不是 stdin

发布于 2024-09-11 18:25:54 字数 642 浏览 2 评论 0原文

这就是我正在做的:

  ssh_ = new Process();
  ssh_.StartInfo.FileName = SshProvider;
  ssh_.StartInfo.Arguments = "-t -t " + Environment.UserName + "@" + serverName_;
  ssh_.StartInfo.UseShellExecute = false;
  ssh_.StartInfo.RedirectStandardInput = true;
  ssh_.StartInfo.RedirectStandardOutput = true;
  ssh_.StartInfo.RedirectStandardError = true;
  ssh_.StartInfo.CreateNoWindow = true;
  ssh_.Start();
  new Thread(new ThreadStart(ReadStdOut)).Start();
  new Thread(new ThreadStart(ReadStdErr)).Start();

我想从 stdout (或 stderr)读取密码提示并将密码写入 stdin,但 ssh 正在从我启动单声道应用程序的控制台进行写入和读取。我不明白为什么会这样,因为我已经在上面的代码中重定向了这些流。

This is what I'm doing:

  ssh_ = new Process();
  ssh_.StartInfo.FileName = SshProvider;
  ssh_.StartInfo.Arguments = "-t -t " + Environment.UserName + "@" + serverName_;
  ssh_.StartInfo.UseShellExecute = false;
  ssh_.StartInfo.RedirectStandardInput = true;
  ssh_.StartInfo.RedirectStandardOutput = true;
  ssh_.StartInfo.RedirectStandardError = true;
  ssh_.StartInfo.CreateNoWindow = true;
  ssh_.Start();
  new Thread(new ThreadStart(ReadStdOut)).Start();
  new Thread(new ThreadStart(ReadStdErr)).Start();

I want to read the password prompt from stdout (or stderr) and write the password into stdin, but ssh is writing and reading from the console from which I started my mono app. I don't understand why that is, since I have redirected those streams in the code above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把时间冻结 2024-09-18 18:25:54

ssh 不从 stdin 读取。这是非常有意的行为,专门设计用于防止人们尝试从脚本或其他程序传递密码。他们没有标记来禁用此行为;没有办法改变它。

执行自动登录的预期方法是使用私钥/公钥机制来绕过密码提示。执行此操作的高级步骤是:

  1. 在客户端上运行 ssh-keygen 以创建公钥-私钥对。
  2. 将公钥文件(例如~/.ssh/id_rsa.pub)的内容添加到服务器的授权主机文件(例如~/.ssh/authorized_keys2)。

通过这样做,您授权客户端使用其密钥连接到服务器,因此不需要密码。

ssh does not read from stdin. This is very intentional behavior designed specifically to prevent people from trying to pass in passwords from scripts or other programs. They do not have a flag to disable this behavior; there is no way to change it.

The intended way to perform automated logins is to use the private/public key mechanism to bypass the password prompt. The high level steps to do that are:

  1. Run ssh-keygen on the client to create a public-private key pair.
  2. Add the contents of the public key file (e.g. ~/.ssh/id_rsa.pub) to the server's authorized hosts file (e.g. ~/.ssh/authorized_keys2).

By doing this you authorize the client to connect to the server using its keys so there is no password needed.

放血 2024-09-18 18:25:54

我应该使用像 SharpSSH 或 Granados 这样的 C# SSH 库,而不是尝试与另一个进程交互。

I should be using a C# SSH library like SharpSSH or Granados, rather than trying to interact with another process.

﹎☆浅夏丿初晴 2024-09-18 18:25:54

请查看解决此问题的 python pexpect。

pexpect.sourceforge.net

http:// linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/

Please have a look on python pexpect that solves this issue.

pexpect.sourceforge.net

http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/

Taharqa

自由范儿 2024-09-18 18:25:54

实际情况比这稍微复杂一些。

SSH 和其他应用程序期望为其“输入”提供比字节流更多的服务,它们需要终端处于活动状态。

终端提供各种服务,例如打开和关闭字符“echo”(关闭它以输入密码,打开它以进行常规操作)。能够捕获某些控制序列(有选择地忽略中断或挂起信号),收到显示屏上的变化通知(ssh需要这个,这样它才能反过来通知远程端终端大小发生了变化,并且您的远程编辑器可以正确地重新显示本身)。

Unix 提供了一种名为“伪终端”(缩写为“pty”)的服务来提供此功能,而 Mono 有一个 .NET 绑定,可让您使用伪终端控制进程,您可以在此处找到代码:

http://github.com/mono/pty-sharp

其中包含一个示例。

It is a little bit more complicated than that.

SSH and other applications expect a few more services for their "input" than a stream of bytes, they require a terminal to be active.

A terminal offers various services, like turning on and off the character "echo" (you turn it off to enter passwords, turn it on for regular operation). Be able to capture certain control sequences (selectively ignore interrupt or suspend signals), be notified of changes on the display screen (ssh needs this so it can in turn notify the remote end that the terminal size changed, and your remote editor can properly redisplay itself).

Unix provides a service called "pseudo terminals" (shortened as "pty") that provide this functionality and Mono has a .NET binding that lets you control processes using pseudo terminals, you can find the code here:

http://github.com/mono/pty-sharp

There is a sample included there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文