如何在 C# 中使用 SSH.NET 库连接 IPv6 地址

How to connect with an IPv6 address using SSH.NET library in C#

我想通过 SSH 连接到远程 GNU+Linux 机器并执行命令,但使用 IPv6 地址。使用 IPv4 地址,它工作得很好。

我 运行 在 Ubuntu 15.04 上使用 MonoDevelop (Mono 4.0)

这是我使用 Renci SSH.NET 库的示例代码:

SshClient sshClient = new SshClient (ipV6Address, user, pass);
sshClient.Connect ();
SshCommand sshCmd = sshClient.RunCommand (command);
sshClient.Disconnect ();

我也尝试过使用 Tamirs SshSharp 和终端控制。

[ ] 添加到 IPv6 地址也不起作用。

我得到以下异常:

System.Net.Sockets.SocketException: Invalid arguments
at System.Net.Sockets.Socket+SocketAsyncResult.CheckIfThrowDelayedException () [0x00000] in <filename unknown>:0  
at System.Net.Sockets.Socket.EndConnect (IAsyncResult result) [0x00000] in <filename unknown>:0 
at Renci.SshNet.Session.SocketConnect (System.String host, Int32 port) [0x00000] in <filename unknown>:0 
at Renci.SshNet.Session.Connect () [0x00000] in <filename unknown>:0 
at Renci.SshNet.BaseClient.Connect () [0x00000] in <filename unknown>:0 
at main program...

也许有人可以为我提供一个代码示例(用 C# 编写)或告诉我一个明确支持 IPv6 地址的库。该库应该是免费的(最好有 Apache 许可)并且可用于商业用途。

问候 Wima

编辑:

在得出我的 ipAddress 缺少网络接口的结论后,我将 ipAddress 更改为 "fe80::xxxx:xxxx:xxxx:xxxx%2"(是的,2 是正确的,我用 ping6 进行了测试)。

这似乎有点效果"more",因为异常变成了:

System.ArgumentException: host
at Renci.SshNet.ConnectionInfo..ctor (System.String host, Int32 port, System.String username, ProxyTypes proxyType, System.String proxyHost, Int32 proxyPort, System.String proxyUsername, System.String proxyPassword, Renci.SshNet.AuthenticationMethod[] authenticationMethods) [0x00000] in <filename unknown>:0 
at Renci.SshNet.PasswordConnectionInfo..ctor (System.String host, Int32 port, System.String username, System.Byte[] password, ProxyTypes proxyType, System.String proxyHost, Int32 proxyPort, System.String proxyUsername, System.String proxyPassword) [0x00000] in <filename unknown>:0 
at Renci.SshNet.PasswordConnectionInfo..ctor (System.String host, Int32 port, System.String username, System.String password) [0x00000] in <filename unknown>:0 
at Renci.SshNet.SshClient..ctor (System.String host, Int32 port, System.String username, System.String password) [0x00000] in <filename unknown>:0 
at Renci.SshNet.SshClient..ctor (System.String host, System.String username, System.String password) [0x00000] in <filename unknown>:0 
at main program...

已解决

使用SSH.NET的预发布包解决了我的问题!

感谢@MartinPrikryl 和其他人 ;)

如果连接到 link 本地地址 (FE80::),则需要使用 % 指定接口名称,例如 FE80::%eth0

因为您可以有多个 link 本地地址并且您需要告诉您的 OS 使用哪个接口。对于单播地址,OS 由于路由 table 知道要使用哪个接口。