I'm trying to do the following thing in a C# program with SSH.NET :
ssh -NfD 1080 username@remote.com
Here's the code I produced :
using (var client = new SshClient("remote.com", "username", "password"))
{
client.Connect();
var port = new ForwardedPortLocal("localhost", 1080, "remote.com", 1080);
client.AddForwardedPort(port);
port.Exception += delegate(object sender, ExceptionEventArgs e)
{
Console.WriteLine(e.Exception.ToString());
};
port.Start();
}
Console.ReadKey();
I connect to an OpenVPN server through this tunnel. When I use the command line it works fine, but when I use the C# program it's like the tunnel isn't working, even if I can send commands to the server I'm connected to through the C# program.
Any idea ?
EDIT :
I found an answer thanks to da_rinkes on SSH.NET forum (link).
I was using ForwardedPortLocal instead of ForwardedPortDynamic (-D option with ssh command).
Here's the new code :
public void Start()
{
using (var client = new SshClient("remote.com", "username", "password"))
{
client.KeepAliveInterval = new TimeSpan(0, 0, 30);
client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20);
client.Connect();
ForwardedPortDynamic port = new ForwardedPortDynamic("127.0.0.1", 1080);
client.AddForwardedPort(port);
port.Exception += delegate(object sender, ExceptionEventArgs e)
{
Console.WriteLine(e.Exception.ToString());
};
port.Start();
System.Threading.Thread.Sleep(1000 * 60 * 60 * 8);
port.Stop();
client.Disconnect();
}
this.Start();
}
Still have to find an other way to keep the SSH tunnel up (instead of a Sleep + recursive call every 8 hours).
Hope it helps someone. Thank you faby !
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…