Add and remove tunnels on the fly
To understand this article better, we suggest that you get yourself familiar with Work with multiple tunnels first, which explains how to route multiple tunnels through a single SSH connection.
Simple forwarding components allow you to create and cancel tunnels on the fly, while the SSH session is active. This may be useful where you need to add new or re-configure old tunnels in response to some user actions or changed operating circumstances without re-starting the SSH session.
Essentially, this is all about managing the Tunnels list (get_Tunnels() method on certain platforms). To add a new tunnel on the fly, you use AddTunnel() method:
idx = forwarding.AddTunnel(); forwarding.get_Tunnels(idx).ForwardedHost = ""; forwarding.get_Tunnels(idx).ForwardedPort = "443"; forwarding.get_Tunnels(idx).DestHost = "server.com"; forwarding.get_Tunnels(idx).DestPort = 443;
If you are running the above code while the session is active, the tunnel is not opened automatically upon creation. This is to let you set up its properties as needed. Once you've set up all the needed settings, call the tunnel object's Open() method:
This will initiate tunnel setup. You will be notified about the outcome with OnTunnelOpen event.
To close an active tunnel, call its Close() method. If you do not need the tunnel object any more, remove it from the list with forwarding.RemoveTunnel() method. Note that you can leave the tunnel in the Tunnels list after closing it and re-open it again later.