Sample code for 30+ languages & platforms
Visual FoxPro

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

Visual FoxPro
LOCAL lnSuccess
LOCAL loSsh
LOCAL lnSshPort
LOCAL lcPassword
LOCAL lnChannelNum

lnSuccess = 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.

loSsh = CreateObject('Chilkat.Ssh')

lnSshPort = 22
lnSuccess = loSsh.Connect("ssh.example.com",lnSshPort)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  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.
lcPassword = "mySshPassword"

lnSuccess = loSsh.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  Open a session channel.  A negative return value indicates failure.
lnChannelNum = loSsh.OpenSessionChannel()
IF (lnChannelNum < 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

IF (loSsh.ChannelIsOpen(lnChannelNum)) THEN
    ? "The channel is open."
ELSE
    ? "The channel is not open."
ENDIF

*  Send CLOSE, which immediately marks the channel locally closed.
lnSuccess = loSsh.ChannelSendClose(lnChannelNum)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

IF (loSsh.ChannelIsOpen(lnChannelNum)) THEN
    ? "Still open."
ELSE
    ? "The channel is now closed."
ENDIF

loSsh.Disconnect()

RELEASE loSsh