通过C#中的SSH.NET进行多跳SSH [英] Multi hop SSH through SSH.NET in C#

查看:248
本文介绍了通过C#中的SSH.NET进行多跳SSH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对一台Linux计算机进行SSH,然后又想从那里对另一台Linux计算机进行SSH,以执行一些Perforce任务。

 使用(SshClient ssh = new SshClient( ip address, username, pwd))
{
ssh.Connect();
命令= ssh.CreateCommand( ssh主机名);
结果= command.Execute();
Console.WriteLine(result);
}

ssh主机名密码少的ssh。如何控制第二个SSH会话并将命令传递给它?



甚至探讨了 CreateShell 函数,但似乎不建议将其用于自动化。

解决方案

通常,尝试自动化 ssh 命令是一个错误的设计。



您最好使用端口转发(也称为SSH隧道)来实现跃点。

  var firstClient = 
新的SshClient(firstHostName,firstUserName,firstPassword);
firstClient.Connect();

var port = new ForwardedPortLocal( 127.0.0.1,secondHostName,22);
firstClient.AddForwardedPort(port);
port.Start();

var secondClient =
new SshClient(port.BoundHost,(int)port.BoundPort,secondUserName,secondPassword);
secondClient.Connect();

var命令= secondClient.CreateCommand( ls);
var result = command.Execute();
Console.WriteLine(result);






在某些情况下,自动执行 ssh 是可以接受的(尽管仍然不理想)。例如。因为对第一台主机上设置的第二台主机进行了身份验证。即 .ssh 文件夹中可能有私钥,并且您不允许将该密钥转移到客户端计算机上。



<即使是这样,也请尝试与系统管理员联系,以找到更好的解决方案。仍然可以使用您的应用程序中包含的凭据来访问私钥,因此,如果私钥本身直接包含在应用程序中,那么它就没有受到更好的保护。



ssh 可以在其命令行上接受命令,例如:

  command = ssh.CreateCommand( ssh主机名命令); 
结果= command.Execute();
Console.WriteLine(result);


I am doing SSH to a Linux machine and again from there want to SSH to another Linux machine to carry out few Perforce tasks.

using (SshClient ssh = new SshClient("ip address","username", "pwd"))
{
    ssh.Connect();
    command = ssh.CreateCommand("ssh hostname");
    result = command.Execute();
    Console.WriteLine(result);
}

Where the ssh hostname is a password less ssh. How can I control the second SSH session and pass commands to it?

Even explored the CreateShell function, but seems like it is not suggested for automation.

解决方案

In general, trying to automate ssh command is a bad design.

You better use a port forwarding (aka SSH tunnel) to implement the "hop".

var firstClient =
    new SshClient(firstHostName, firstUserName, firstPassword);
firstClient.Connect();

var port = new ForwardedPortLocal("127.0.0.1", secondHostName, 22);
firstClient.AddForwardedPort(port);
port.Start();

var secondClient =
    new SshClient(port.BoundHost, (int)port.BoundPort, secondUserName, secondPassword);
secondClient.Connect();

var command = secondClient.CreateCommand("ls");
var result = command.Execute();
Console.WriteLine(result);


There are some cases, when automating the ssh is acceptable (while still not ideal). E.g. because there's an authentication to the second host set up on the first one. I.e. there might be private key in the .ssh folder and you are not allowed to transfer that key to your client machine.

Even then, try talking to the system Administrator to find a better solution. The private key is still accessible using the credentials contained in your application, so it's not protected any better, had the private key itself been contained directly in the application.

Anyway, ssh can accept a command on its command line, like:

command = ssh.CreateCommand("ssh hostname command");
result = command.Execute();
Console.WriteLine(result);

这篇关于通过C#中的SSH.NET进行多跳SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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