SSH.NET icon indicating copy to clipboard operation
SSH.NET copied to clipboard

Equivalent code to plink command

Open aberbegall opened this issue 9 years ago • 6 comments

Hi I am trying to replicate this plink command:

plink.exe -L 5900:localhost:5900 -i key.ppk user@remotehost

with SSH.NET like this:

var keyfile = new PrivateKeyFile(@"key.ppk");
var method = new PrivateKeyAuthenticationMethod("user", keyfile);
var connectinfo = new ConnectionInfo(remotehost, "user", method);
using (var client = new SshClient(connectinfo))
{
    client.Connect();
    var port = new ForwardedPortLocal("localhost", 5900, "remotehost", 5900);
    client.AddForwardedPort(port);
    port.Exception += delegate(object sender, ExceptionEventArgs e) { 
    Console.WriteLine(e.Exception.ToString()); };
    port.Start();
    ...
}

but it does not work and it does not throw any exception with the latest beta release (from Nuget).

Any ideas? Am I missing something?

Thanks

aberbegall avatar Apr 08 '17 17:04 aberbegall

@aberbegall : I assume you want to tunnel VNC port from the remote box to your workstation via SSH.

Quick and dirty example:

ConnectionInfo connection = new ConnectionInfo("192.168.1.113", 22, "kasbst", 
                    new PasswordAuthenticationMethod("kasbst", "password"));
                
using (SshClient ubuntubox = new SshClient(connection))
{
        ubuntubox.Connect();

        var port = new ForwardedPortLocal("127.0.0.1", 5900, "192.168.1.113", 5901);
        ubuntubox.AddForwardedPort(port);
        port.Exception += delegate (object sender, ExceptionEventArgs e) {
                Console.WriteLine(e.Exception.ToString());
        };
        port.Start();
                    
        if (ubuntubox.IsConnected && port.IsStarted)
        {
               Console.WriteLine("Connected and tunnel established! Use localhost::5900 " +
                   "in your vncviewer to connect");
        }
        Console.WriteLine("Press any key to close the tunnel!");
        // Keep the tunnel opened for demonstration
        Console.ReadKey();

        port.Stop();
        ubuntubox.RemoveForwardedPort(port);
        ubuntubox.Disconnect();
}

console

vnc

Let me know if you need more help.

kasbst avatar May 09 '17 20:05 kasbst

@kasbst sorry for my late feedback Please notice I am using a private Key file and PrivateKeyAuthenticationMethod and you used a Password in your example.

If I run

netstat -anob 

I can see that the connection with the remote host was ESTABLISHED on port 22 and the locally is LISTENING on the specified port as expected.

No exception is thrown but it simply does not work :(

aberbegall avatar Aug 10 '17 09:08 aberbegall

AFAIR ppk-Files (Putty private key) wont work as private keys.

src/Renci.SshNet/PrivateKeyFile.cs /// Supports RSA and DSA private key in both OpenSSH and ssh.com format.

Edit: oops! forgot the not

darinkes avatar Aug 10 '17 13:08 darinkes

Ppk files are formatted differently. On the error of permission denied is the key protected via a passphrase?

On Aug 10, 2017, at 9:24 AM, Stefan Rinkes [email protected] wrote:

AFAIR ppk-Files (Putty private key) work as private keys.

src/Renci.SshNet/PrivateKeyFile.cs /// Supports RSA and DSA private key in both OpenSSH and ssh.com format.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sshnet/SSH.NET/issues/201#issuecomment-321549778, or mute the thread https://github.com/notifications/unsubscribe-auth/AAf0HqDdo7TxfuoJO6nrzXeC44bnHxOOks5sWwSUgaJpZM4M3xf-.

darkoperator avatar Aug 10 '17 13:08 darkoperator

@darinkes I used a ssh.com key format, so it's not plink format. But this does not solve the problem. @darkoperator this is not a problem anymore, it's solved... I don't get any error/exception it just does not work.

aberbegall avatar Aug 10 '17 15:08 aberbegall

Does the ssh.net expect to somehow acceptance of the server host key? I wonder whether I am missing the client acceptance of the server fingerprint, similar like pink when prompts to accept a key fingerprint for a particular host the first time it runs and stores it on its registry cache. Or perhaps I need to add the host IP to the trusted hosts, I am just thinking out loud since I have no clue where the issue is.

aberbegall avatar Aug 11 '17 05:08 aberbegall