PowerBuilder
PowerBuilder
Check Whether an SSH Channel is Open
See more SSH Examples
Demonstrates the Chilkat Ssh.ChannelIsOpen method, which reports whether a channel number identifies a channel Chilkat currently considers open for application I/O. The only argument is the channel number. This example checks the channel before and after sending CLOSE.
Background: Because one SSH connection can carry many channels with independent lifetimes, code that manages several needs a way to ask whether a given one is still usable. This returns false for invalid or nonexistent channel numbers, after a local
Disconnect, once remote closure is processed, and immediately after ChannelSendClose — which marks the channel locally closed without waiting for the remote side.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
li_Success = 0
// Demonstrates the Ssh.ChannelIsOpen method, which reports whether a channel number identifies
// a channel Chilkat currently considers open for application I/O. The only argument is the
// channel number.
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
li_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
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
// Open a session channel. A negative return value indicates failure.
li_ChannelNum = loo_Ssh.OpenSessionChannel()
if li_ChannelNum < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
if loo_Ssh.ChannelIsOpen(li_ChannelNum) then
Write-Debug "The channel is open."
else
Write-Debug "The channel is not open."
end if
// Send CLOSE, which immediately marks the channel locally closed.
li_Success = loo_Ssh.ChannelSendClose(li_ChannelNum)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
if loo_Ssh.ChannelIsOpen(li_ChannelNum) then
Write-Debug "Still open."
else
Write-Debug "The channel is now closed."
end if
loo_Ssh.Disconnect()
destroy loo_Ssh