DataFlex
DataFlex
TCP or TLS over Multiple Hop SSH to Remote Server
See more Socket/SSL/TLS Examples
Demonstrates how to use the Chilkat Socket API to connect to a remote server (using TCP or TLS) tunneled through mulitple-hop SSH. The scheme looks like this:Application => ServerSSH1 => ServerSSH2 => DestinationServer
The ConnectThroughSsh and UseSsh methods are added in Chilkat version 9.5.0.55 to accomplish this task.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vSsh1
Handle hoSsh1
Variant vSsh2
Handle hoSsh2
Handle hoSocket
Boolean iUseTls
Integer iMaxWaitMillisec
String sResponseStatusLine
String sResponseHeaderLine
String sRemainderOfHeader
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatSsh)) To hoSsh1
If (Not(IsComObjectCreated(hoSsh1))) Begin
Send CreateComObject of hoSsh1
End
// Connect directly to the 1st SSH server.
Get ComConnect Of hoSsh1 "serverssh1.com" 22 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSsh1 To sTemp1
Showln sTemp1
Procedure_Return
End
// Authenticate using login/password:
Get ComAuthenticatePw Of hoSsh1 "ssh1Login" "ssh1Password" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSsh1 To sTemp1
Showln sTemp1
Procedure_Return
End
// Connect through the 1st SSH connection to reach a 2nd SSH server.
// Note: Any number of SSH connections may be simultaneously tunneled through a single
// existing SSH connection.
Get Create (RefClass(cComChilkatSsh)) To hoSsh2
If (Not(IsComObjectCreated(hoSsh2))) Begin
Send CreateComObject of hoSsh2
End
Get pvComObject of hoSsh1 to vSsh1
Get ComConnectThroughSsh Of hoSsh2 vSsh1 "serverssh2.com" 22 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSsh2 To sTemp1
Showln sTemp1
Procedure_Return
End
// Authenticate with ssh2...
Get ComAuthenticatePw Of hoSsh2 "ssh2Login" "ssh2Password" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSsh2 To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatSocket)) To hoSocket
If (Not(IsComObjectCreated(hoSocket))) Begin
Send CreateComObject of hoSocket
End
// Tell the socket object to connect to our destination server though the ssh2 tunnel (which itself is routed through ssh1).
// The connection looks like this: ApplicationSocket(TLS) => ServerSSH1 => ServerSSH2 => DestinationServer
Get pvComObject of hoSsh2 to vSsh2
Get ComUseSsh Of hoSocket vSsh2 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSocket To sTemp1
Showln sTemp1
Procedure_Return
End
// Connect using TLS to www.chilkatsoft.com
// We could also tunnel a bare TCP connection by specifying port 80 with useTls = False.
Move True To iUseTls
Move 20000 To iMaxWaitMillisec
Get ComConnect Of hoSocket "www.chilkatsoft.com" 443 iUseTls iMaxWaitMillisec To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSocket To sTemp1
Showln sTemp1
Procedure_Return
End
// Once the multiple hop SSH tunneled connection is setup, the socket programming
// is identical to the normal case where we have a direct connection.
// Tell the socket object that all text is to be sent in the utf-8 encoding,
// and the text received is assumed to be utf-8.
Set ComStringCharset Of hoSocket To "utf-8"
// Send an HTTP HEAD request:
Get ComSendString Of hoSocket "HEAD / HTTP/1.1" + (character(13)) + (character(10)) + "Host: www.chilkatsoft.com" + (character(13)) + (character(10)) + (character(13)) + (character(10)) To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSocket To sTemp1
Showln sTemp1
Procedure_Return
End
// Wait a maximum of 4 seconds while no data is forthcoming:
Set ComMaxReadIdleMs Of hoSocket To 4000
// Get the 1st response line, which should be "HTTP/1.1 200 OK"
Get ComReceiveToCRLF Of hoSocket To sResponseStatusLine
Get ComLastMethodSuccess Of hoSocket To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoSocket To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "StatusLine: " sResponseStatusLine
// Now get the 1st line of the response header:
Get ComReceiveToCRLF Of hoSocket To sResponseHeaderLine
Get ComLastMethodSuccess Of hoSocket To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoSocket To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "HeaderLine: " sResponseHeaderLine
// Now read the remainder of the response header by reading until a double CRLF is seen:
Get ComReceiveUntilMatch Of hoSocket (character(13)) + (character(10)) + (character(13)) + (character(10)) To sRemainderOfHeader
Get ComLastMethodSuccess Of hoSocket To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoSocket To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Remainder: " sRemainderOfHeader
// Close the connection with the server. This closes the tunnel through ssh2.
// Wait a max of 20 seconds (20000 millsec)
Get ComClose Of hoSocket 20000 To iSuccess
// Close the connection with ssh2. (This closes the the tunnel through ssh1.)
// The connection with ssh1 is still alive, and may be used for more connections.
Send ComDisconnect To hoSsh2
Send ComDisconnect To hoSsh1
End_Procedure