Wima
Wima

Reputation: 43

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

I want to connect via SSH to a remote GNU+Linux machine and execute a command, but with an IPv6 address. With an IPv4 address it works perfectly fine.

I am running on Ubuntu 15.04 and use MonoDevelop (Mono 4.0)

Here is my sample code using Renci SSH.NET library:

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

I also tried to use Tamirs SshSharp and Terminal Control.

Adding [ ] to the IPv6 address didn't work either.

I get following exception:

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...

Maybe someone could provide me a code example (in C#) or tell me a library that clearly supports IPv6 addresses. The library should be free (preferably with Apache license) and usable for commercial use.

Greetings Wima

EDIT:

After coming to the conclusion that my ipAddress lacks the network interface I changed my ipAddress to "fe80::xxxx:xxxx:xxxx:xxxx%2" (yes 2 is the right one, I tested with ping6).

This seems to work a bit "more", because the exception changed into:

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...

SOLVED

Using the pre-release package of SSH.NET solved my problem!

Thanks to @MartinPrikryl and others ;)

Upvotes: 4

Views: 2390

Answers (1)

user3193469
user3193469

Reputation: 124

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.

Upvotes: 3

Related Questions