Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement -Nf and sleep #1566

Open
wuq5789 opened this issue Jan 3, 2025 · 3 comments
Open

How to implement -Nf and sleep #1566

wuq5789 opened this issue Jan 3, 2025 · 3 comments

Comments

@wuq5789
Copy link

wuq5789 commented Jan 3, 2025

First of all, thanks to ssh.net for such a handy library.

ssh.net how do I implement the following functions of a common SSH client?
ssh -NfL 5555:127.0.0.1:6666 [email protected] sleep 30

The focus is on how the functions of -Nf and sleep are implemented.

@Rob-Hague
Copy link
Collaborator

We have for the ssh process:

-f

Requests ssh to go to background just before command execution. [...]

Port forwarding in SSH.NET runs in its own thread(s), in effect it would already work in the background.

-N

Do not execute a remote command. This is useful for just forwarding ports. Refer to the description of SessionType in ssh_config(5) for details.

I could be wrong but I think this would mean the sleep command is ignored.

We are left with local port forwarding, something like (untested, adapted from tests in https://github.com/sshnet/SSH.NET/blob/9e1ee0a380873a485a90e7c5778ed83f8dc44a15/test/Renci.SshNet.IntegrationTests/OldIntegrationTests/ForwardedPortLocalTest.cs):

using (var client = new SshClient("host", "port", "user", "pwd"))
{
    client.Connect();

    using var port = new ForwardedPortLocal("127.0.0.1", 5555, "127.0.0.1", 6666);

    port.Exception += (object sender, ExceptionEventArgs e) =>
    {
        Console.Error.WriteLine(e.Exception.ToString());
    };

    port.RequestReceived += (object sender, PortForwardEventArgs e) =>
    {
        Console.WriteLine($"Connection from {e.OriginatorHost}:{e.OriginatorPort}");
    };

    client.AddForwardedPort(port);
    port.Start();

    // Sleep here as an example, in practice store the client/port somewhere
    // and close it all down when desired
    Thread.Sleep(30 * 1000);

    client.RemoveForwardedPort(port);
    client.Disconnect();
}

Hope it helps!

@wuq5789
Copy link
Author

wuq5789 commented Jan 9, 2025

Thank you for your reply
The sleep command is to turn off port forwarding if there is no network data for a certain period of time (e.g., 60 seconds).
Rather than turning off port forwarding after a certain amount of time, as you would understand

@Rob-Hague
Copy link
Collaborator

Ok I see, I don't think there is a pre-built way to achieve that. I imagine you might need to hook into the internal code which reads from the local socket

SocketAbstraction.ReadContinuous(_socket, buffer, 0, buffer.Length, SendData);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants