SSH tunnel(port forwarding) is not working with library but working with putty

Apr 25, 2016 at 2:31 PM
Edited Apr 25, 2016 at 2:40 PM
Hi,
I'm trying to build SSH tunnel through an external ssh server, meaning:
forward some ports as local from my machine to ssh server, and then forward remote ports from ssh to different ports on my machine)
This is working perfectly when I use putty using the following command:
putty -ssh ubuntu@...remoteSshServerIp... -i "@...Path to key...@" -R 9000:localhost:7777 -R 9001:localhost:8080 -R 9002:localhost:8082 -L 12120:localhost:9000 -L 12121:localhost:9001 -L 12122:localhost:9002 
(meaning I want to access port 12120 on browser and actually get result from port 7777)

When trying to accomplish that using ssh.net library(obviously using the same local and remote machines and same ports) with the below code(using console application with ssh.net) I get the following exception:
remotePort exceptionSystem.Net.Sockets.SocketException (0x80004005): No connecti
on could be made because the target machine actively refused it [::1]:7777
  at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
  at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
  at Renci.SshNet.Channels.ChannelForwardedTcpip.OpenSocket(IPAddress connected
Host, UInt32 connectedPort)
  at Renci.SshNet.Channels.ChannelForwardedTcpip.Bind(IPAddress connectedHost,
UInt32 connectedPort)
  at Renci.SshNet.ForwardedPortRemote.<>c__DisplayClass2.<Session_ChannelOpenin
g>b__0()
Here is the code I'm using:
static void Main(string[] args)
        {
            PrivateKeyFile file = new PrivateKeyFile(@"...Path to key...");
            client = new SshClient("...remoteSshServerIp...", "ubuntu", file);

            client.Connect();

            var localPort1 = CreateLocalPort(12120, 9000);
            var localPort2 = CreateLocalPort(12121, 9001);
            var localPort3 = CreateLocalPort(12122, 9002);
            client.AddForwardedPort(localPort1);
            client.AddForwardedPort(localPort2);
            client.AddForwardedPort(localPort3);
            localPort1.Start();
            localPort2.Start();
            localPort3.Start();

            var remotePort1 = CreateRemotePort(9000,7777);
            var remotePort2 = CreateRemotePort(9001,8080);
            var remotePort3 = CreateRemotePort(9002,8082);
            client.AddForwardedPort(remotePort1);
            client.AddForwardedPort(remotePort2);
            client.AddForwardedPort(remotePort3);
            remotePort1.Start();
            remotePort2.Start();
            remotePort3.Start();

            Console.ReadKey();
        }
        private static ForwardedPortRemote CreateRemotePort(uint from, uint to)
        {
            var remotePort = new ForwardedPortRemote("localhost", from, "localhost", to);
            remotePort.RequestReceived += delegate (object sender, PortForwardEventArgs e) { Console.WriteLine(e.OriginatorHost + ":" + e.OriginatorPort); };
            remotePort.Exception += delegate (object sender, ExceptionEventArgs e) { Console.WriteLine("remotePort exception" + e.Exception); };
            return remotePort;
        }
        private static ForwardedPortLocal CreateLocalPort(uint from,uint to)
        {
            var localPort = new ForwardedPortLocal("localhost", from, "localhost", to);
            localPort.RequestReceived += delegate (object sender, PortForwardEventArgs e) { Console.WriteLine(e.OriginatorHost + ":" + e.OriginatorPort); };
            localPort.Exception += delegate (object sender, ExceptionEventArgs e) { Console.WriteLine("localPort exception" + e.Exception); };
            return localPort;
        }
Any ideas?

Thanks,