C# 用C显示完整的SSH外壳输出,包括登录消息和提示
我正在尝试用C创建一个简单的SSH客户端。下面是我的代码:C# 用C显示完整的SSH外壳输出,包括登录消息和提示,c#,.net,ssh,ssh.net,C#,.net,Ssh,Ssh.net,我正在尝试用C创建一个简单的SSH客户端。下面是我的代码: using Renci.SshNet; public static void Main(string[] args) { AuthenticationMethod method = new PasswordAuthenticationMethod("pi", "raspberry"); ConnectionInfo connection = new ConnectionInfo("192.168.91.134", "p
using Renci.SshNet;
public static void Main(string[] args)
{
AuthenticationMethod method = new PasswordAuthenticationMethod("pi", "raspberry");
ConnectionInfo connection = new ConnectionInfo("192.168.91.134", "pi", method);
SshClient client = new SshClient(connection);
if (!client.IsConnected)
{
Console.WriteLine("Not Connected...");
client.Connect();
}
while (true)
{
string command = Console.ReadLine();
SshCommand response = client.RunCommand(command);
Console.WriteLine(response.Result);
}
}
问题:
像这样,它只显示发送的命令的响应。我想要完整的输出,还有用户和当前目录,就像一个经典的SSH shell一样
如果我想启动sudo su命令并给出密码,它就不起作用了。。。
将来,我想将输出添加到列表框中,并从winForms应用程序中的文本框中获取输入
提前感谢您的帮助如果要实现PuTTY这样的SSH终端客户端,您需要使用SSH外壳通道
在SSH.NET中,使用SshClient.CreateShell或SshClient.CreateShellStream,而不是SshClient.RunCommand。如果要实现像PuTTY这样的SSH终端客户端,需要使用SSH外壳通道
在SSH.NET中,使用SshClient.CreateShell或SshClient.CreateShellStream,而不是SshClient.RunCommand。这是我的实现:
using System;
using System.Threading;
using Renci.SshNet;
public class Program
{
public static void Main(string[] args)
{
SshClient sshClient = new SshClient("192.168.91.134", 22, "pi", "raspberry");
sshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(120);
sshClient.Connect();
ShellStream shellStreamSSH = sshClient.CreateShellStream("vt-100", 80, 60, 800, 600, 65536);
Thread thread = new Thread(() => recvSSHData(shellStreamSSH));
thread.Start();
while (true)
{
string command = Console.ReadLine();
shellStreamSSH.Write(command + "\n");
shellStreamSSH.Flush();
}
}
public static void recvSSHData(ShellStream shellStreamSSH)
{
while (true)
{
try
{
if (shellStreamSSH != null && shellStreamSSH.DataAvailable)
{
string strData = shellStreamSSH.Read();
Console.WriteLine(strData);
}
}
catch
{
}
System.Threading.Thread.Sleep(200);
}
}
}
这是我的实现:
using System;
using System.Threading;
using Renci.SshNet;
public class Program
{
public static void Main(string[] args)
{
SshClient sshClient = new SshClient("192.168.91.134", 22, "pi", "raspberry");
sshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(120);
sshClient.Connect();
ShellStream shellStreamSSH = sshClient.CreateShellStream("vt-100", 80, 60, 800, 600, 65536);
Thread thread = new Thread(() => recvSSHData(shellStreamSSH));
thread.Start();
while (true)
{
string command = Console.ReadLine();
shellStreamSSH.Write(command + "\n");
shellStreamSSH.Flush();
}
}
public static void recvSSHData(ShellStream shellStreamSSH)
{
while (true)
{
try
{
if (shellStreamSSH != null && shellStreamSSH.DataAvailable)
{
string strData = shellStreamSSH.Read();
Console.WriteLine(strData);
}
}
catch
{
}
System.Threading.Thread.Sleep(200);
}
}
}
你们看过2014年的帖子吗?你们看过2014年的帖子吗?我搜索了很长时间,想知道如何做到这一点。我找不到任何文档。其他每一个例子要么展示了如何执行单个命令,要么关注于SFTP。非常感谢你发布这篇文章。它对我非常有效。我现在可以与远程计算机上的bourne shell进行交互。我希望C实现能够发送软件生成的命令以及键入我自己的命令。我找不到任何文档。其他每一个例子要么展示了如何执行单个命令,要么关注于SFTP。非常感谢你发布这篇文章。它对我非常有效。我现在可以与远程计算机上的bourne shell进行交互。我希望C实现能够发送由软件生成的命令,以及键入我自己的命令。