ssh 坚持从控制台读取密码而不是 stdin
这就是我正在做的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ssh 不从 stdin 读取。这是非常有意的行为,专门设计用于防止人们尝试从脚本或其他程序传递密码。他们没有标记来禁用此行为;没有办法改变它。
执行自动登录的预期方法是使用私钥/公钥机制来绕过密码提示。执行此操作的高级步骤是:
~/.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:
ssh-keygen
on the client to create a public-private key pair.~/.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.
我应该使用像 SharpSSH 或 Granados 这样的 C# SSH 库,而不是尝试与另一个进程交互。
I should be using a C# SSH library like SharpSSH or Granados, rather than trying to interact with another process.
请查看解决此问题的 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
实际情况比这稍微复杂一些。
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.