Sample code for 30+ languages & platforms
React Native

Route Socket Connections through an SSH Object

See more Socket/SSL/TLS Examples

Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.

Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.

Chilkat React Native Downloads

React Native
import { Socket, Ssh } from '@chilkat/react-native'

async function chilkatExample() {
  const ssh = new Ssh();

  // Connect and authenticate a standalone SSH object.
  try {
    await ssh.connectAsync('ssh.example.com', 22);
  } catch {
    console.log(ssh.lastErrorText);
    return;
  }

  // The SSH password should come from a secure source rather than being hard-coded.
  const sshPassword = 'mySshPassword';
  try {
    await ssh.authenticatePwAsync('sshUser', sshPassword);
  } catch {
    console.log(ssh.lastErrorText);
    return;
  }

  const socket = new Socket();

  // Route subsequent Connect calls through the already connected and authenticated SSH object.  The
  // destination is reached by SSH port forwarding rather than by a direct TCP connection.
  try {
    socket.useSsh(ssh);
  } catch {
    console.log(socket.lastErrorText);
    return;
  }

  // Connect to the destination through the SSH tunnel.
  const bTls = false;
  const maxWaitMs = 5000;
  try {
    await socket.connectAsync('db.internal', 5432, bTls, maxWaitMs);
  } catch {
    console.log(socket.lastErrorText);
    return;
  }

  console.log('Connected to the destination through the SSH tunnel.');
}