Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSsh
LOCAL lcSshPassword
LOCAL loSocket
LOCAL lnBTls
LOCAL lnMaxWaitMs

lnSuccess = 0

loSsh = CreateObject('Chilkat.Ssh')

*  Connect and authenticate a standalone SSH object.
lnSuccess = loSsh.Connect("ssh.example.com",22)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  The SSH password should come from a secure source rather than being hard-coded.
lcSshPassword = "mySshPassword"
lnSuccess = loSsh.AuthenticatePw("sshUser",lcSshPassword)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

loSocket = CreateObject('Chilkat.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.
lnSuccess = loSocket.UseSsh(loSsh)
IF (lnSuccess = 0) THEN
    ? loSocket.LastErrorText
    RELEASE loSsh
    RELEASE loSocket
    CANCEL
ENDIF

*  Connect to the destination through the SSH tunnel.
lnBTls = 0
lnMaxWaitMs = 5000
lnSuccess = loSocket.Connect("db.internal",5432,lnBTls,lnMaxWaitMs)
IF (lnSuccess = 0) THEN
    ? loSocket.LastErrorText
    RELEASE loSsh
    RELEASE loSocket
    CANCEL
ENDIF

? "Connected to the destination through the SSH tunnel."

RELEASE loSsh
RELEASE loSocket