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

时间:2015-07-28 10:59:35

标签: c# ssh ipv6 ssh.net

我想通过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进行了测试)。

这似乎有点工作&#34;更多&#34;,因为异常改变为:

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和其他人;)

1 个答案:

答案 0 :(得分:2)

If you connect to a link local adress (FE80::) then you need to specify the interface name with a % eg FE80::%eth0

Because you can have multiple link local addresses and you need to tell your OS which interface to use. With unicast addresses the OS knows due to the routing table what interface to use.