Sample code for 30+ languages & platforms
PowerBuilder

SSH Tunnel (Port Forwarding via direct-tcpip Channel)

See more SSH Examples

Demonstrates creating an SSH tunnel to a remote host:port using a direct-tcpip channel. Data written to the channel is forwarded by the SSH server to the target, and data read back is whatever the target sent. This example tunnels a simple HTTP GET request to a web server.

Background: This is SSH local port forwarding at the API level, and the target need not be a web server — it can be a database, a message broker, or any TCP service. The key detail is that the SSH server resolves and connects to the target, not your machine, so the destination only has to be reachable from the server's network. That is what makes tunneling useful for reaching services behind a firewall. Note the receive pattern here: read until the double CRLF that ends an HTTP header, extract exactly that with GetReceivedTextS, then poll briefly for the body, which may already have arrived.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
string ls_Hostname
integer li_Port
string ls_Password
string ls_TargetHost
integer li_TargetPort
integer li_ChannelNum
string ls_HttpReq
integer li_CaseSensitive
string ls_MatchStr
string ls_ResponseHeader
integer li_PollTimeoutMs
integer li_NumBytesRead
string ls_HtmlBody

li_Success = 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Demonstrates creating an SSH tunnel to a remote host:port using a direct-tcpip channel.
//  Data written to the channel is forwarded by the SSH server to the target host:port, and data
//  read from the channel is whatever the target sent back.

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_Ssh
    MessageBox("Error","Connecting to COM object failed")
    return
end if

ls_Hostname = "ssh.example.com"
li_Port = 22
li_Success = loo_Ssh.Connect(ls_Hostname,li_Port)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Normally you would not hard-code the password in source.  You should instead obtain it
//  from an interactive prompt, environment variable, or a secrets vault.
ls_Password = "mySshPassword"

li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Ask the SSH server to connect onward to this host:port.  The target is resolved by the SSH
//  server, not by this computer, so it must be reachable from the server's network.  The target
//  need not be a web server -- it can be any TCP service.
ls_TargetHost = "www.chilkatsoft.com"
li_TargetPort = 80
li_ChannelNum = loo_Ssh.OpenDirectTcpIpChannel(ls_TargetHost,li_TargetPort)
if li_ChannelNum < 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Send a simple HTTP GET request through the tunnel.
ls_HttpReq = "GET /xyz123.html HTTP/1.1~r~nHost: www.chilkatsoft.com~r~n~r~n"
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,ls_HttpReq,"utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  IMPORTANT: Set a read timeout before receiving until a match.  ReadTimeoutMs defaults to 0,
//  which means no limit -- without it, this call waits forever if the received data never
//  contains a match.
loo_Ssh.ReadTimeoutMs = 15000

//  An HTTP response header ends with a blank line, so receive until a double CRLF is seen.
//  The method may read beyond the match, but it stops waiting as soon as the match appears.
li_CaseSensitive = 0
ls_MatchStr = "~r~n~r~n"
li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,ls_MatchStr,"utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  GetReceivedTextS removes everything through and including the match, which is exactly the
//  response header.
ls_ResponseHeader = loo_Ssh.GetReceivedTextS(li_ChannelNum,ls_MatchStr,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug "---- HTTP Response Header ----"
Write-Debug ls_ResponseHeader

//  The body may already have arrived along with the header.  Poll briefly for anything more.
//  A -2 return simply means nothing further arrived, which is not an error here.
li_PollTimeoutMs = 200
li_NumBytesRead = loo_Ssh.ChannelPoll(li_ChannelNum,li_PollTimeoutMs)
if li_NumBytesRead = -1 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_HtmlBody = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug "---- HTML BODY ----"
Write-Debug ls_HtmlBody

li_Success = loo_Ssh.ChannelSendClose(li_ChannelNum)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

loo_Ssh.Disconnect()


destroy loo_Ssh