Sample code for 30+ languages & platforms
Swift

Open a Forwarded Channel through an SSH Tunnel

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshNewChannel, which opens a port-forwarded channel through an authenticated SSH tunnel to a destination host and port, populating a Socket with the connected channel.

Background. Set the ssl argument to true to establish TLS to the destination inside the SSH tunnel. The returned channel socket is then used to send and receive with the destination.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let socket = CkoSocket()!

    //  Connect to the SSH server and initialize an SSH tunneling session.  Port 22 is conventional but
    //  not required.
    success = socket.sshOpenTunnel(sshHostname: "ssh.example.com", sshPort: 22)
    if success == false {
        print("\(socket.lastErrorText!)")
        return
    }

    //  Authenticate the SSH session before opening forwarded channels.
    //  The SSH password should come from a secure source rather than being hard-coded.
    var sshPassword: String? = "mySshPassword"
    success = socket.sshAuthenticatePw(sshLogin: "sshUser", sshPassword: sshPassword)
    if success == false {
        print("\(socket.lastErrorText!)")
        return
    }

    //  Open a port-forwarded channel through the authenticated SSH tunnel to the destination host and
    //  port, populating a Socket with the connected channel.  Set the 3rd argument to true to establish
    //  TLS to the destination inside the tunnel.
    var bTls: Bool = false
    var maxWaitMs: Int = 20000
    let channel = CkoSocket()!
    success = socket.sshNewChannel(hostname: "db.internal", port: 5432, ssl: bTls, maxWaitMs: maxWaitMs, channel: channel)
    if success == false {
        print("\(socket.lastErrorText!)")
        return
    }

    //  Use the channel socket to send and receive with the destination through the tunnel.
    print("Forwarded channel opened to the destination.")

}