I assume your scenario looks like this:
client <------> server <------> modbus device
Where the communication between client and server uses SSH and the communication between the server and the modbus device uses unencrypted TCP.
In this case, you would have to configure the server to allow outgoing SSH tunneling (unless it's enabled by default) and use Rebex Terminal Emulation's Ssh
object to establish a tunnel:
// connect and log in to an SSH server
var ssh = new Rebex.Net.Ssh();
ssh.Connect(hostname);
ssh.Login(username, password);
// create tunneling rule
// to accept connections at localhost:8502
// and tunnel then over SSH session through the SSH server
// to the specified Modbus device
SshTunnel tunnel = ssh.StartOutgoingTunnel(
"localhost", 8502, // client-side source address
"modbus_device_addresss", 502); // server-side target address
// use NModbus4 here - connect to localhost:8502 in order use the SSH tunnel
...
// stop tunneling
tunnel.Close();
// close SSH connection
ssh.Disconnect();