我试图在C#程序中使用SSH.NET执行以下操作:
ssh -NfD 1080 username@remote.com
下面是我编写的代码:
using (var client = new SshClient("remote.com", "username", "password"))
{
client.Connect();
var port = new ForwardedPortLocal("localhost", 1080, "remote.com", 1080);
client.AddForwardedPort(port);
port.Exception += delegate(object sender, ExceptionEventArgs e)
{
Console.WriteLine(e.Exception.ToString());
};
port.Start();
}
Console.ReadKey();
我通过这个隧道连接到一个OpenVPN服务器。当我使用命令行时,它工作得很好,但是当我使用C#程序时,它就像隧道不能工作一样,即使我可以通过C#程序向连接到的服务器发送命令。知道吗?
发布于 2014-12-11 12:05:00
正如我在注释中建议的那样,我认为您可以尝试以这种方式使用client.RunCommand
方法执行命令
client.RunCommand("some command");
发布于 2016-12-15 06:53:43
应该将Console.ReadKey();放在使用块中,以确保不释放SshClient实例。
static void Main(string[] args)
{
using (var client = new SshClient("host", "name", "pwd"))
{
client.Connect();
var port = new ForwardedPortDynamic(7575);
client.AddForwardedPort(port);
port.Exception += delegate (object sender, ExceptionEventArgs e)
{
Console.WriteLine(e.Exception.ToString());
};
port.Start();
Console.ReadKey();
}
}
https://stackoverflow.com/questions/27421094
复制相似问题