Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local ssh = chilkat.Ssh.new()

local sshPort = 22
local success = ssh:connect("ssh.example.com", sshPort)
if not success then
    print(ssh.last_error_text)
    return
end

-- 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.
local password = "mySshPassword"

success = ssh:authenticate_pw("mySshLogin", password)
if not success then
    print(ssh.last_error_text)
    return
end

-- Open a session channel.  A negative return value indicates failure.
local channelNum = ssh:open_session_channel()
if channelNum < 0 then
    print(ssh.last_error_text)
    return
end

if ssh:channel_is_open(channelNum) then
    print("The channel is open.")
else
    print("The channel is not open.")
end

-- Send CLOSE, which immediately marks the channel locally closed.
success = ssh:channel_send_close(channelNum)
if not success then
    print(ssh.last_error_text)
    return
end

if ssh:channel_is_open(channelNum) then
    print("Still open.")
else
    print("The channel is now closed.")
end

ssh:disconnect()