PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkSsh.pb"
IncludeFile "CkSocket.pb"
Procedure ChilkatExample()
success.i = 0
ssh.i = CkSsh::ckCreate()
If ssh.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect and authenticate a standalone SSH object.
success = CkSsh::ckConnect(ssh,"ssh.example.com",22)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
; The SSH password should come from a secure source rather than being hard-coded.
sshPassword.s = "mySshPassword"
success = CkSsh::ckAuthenticatePw(ssh,"sshUser",sshPassword)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
socket.i = CkSocket::ckCreate()
If socket.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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.
success = CkSocket::ckUseSsh(socket,ssh)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSsh::ckDispose(ssh)
CkSocket::ckDispose(socket)
ProcedureReturn
EndIf
; Connect to the destination through the SSH tunnel.
bTls.i = 0
maxWaitMs.i = 5000
success = CkSocket::ckConnect(socket,"db.internal",5432,bTls,maxWaitMs)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSsh::ckDispose(ssh)
CkSocket::ckDispose(socket)
ProcedureReturn
EndIf
Debug "Connected to the destination through the SSH tunnel."
CkSsh::ckDispose(ssh)
CkSocket::ckDispose(socket)
ProcedureReturn
EndProcedure