JavaScript
JavaScript
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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
// 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.
var ssh = new CkSsh();
var sshPort = 22;
success = ssh.Connect("ssh.example.com",sshPort);
if (success == false) {
console.log(ssh.LastErrorText);
return;
}
// 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.
var password = "mySshPassword";
success = ssh.AuthenticatePw("mySshLogin",password);
if (success == false) {
console.log(ssh.LastErrorText);
return;
}
// Open a session channel. A negative return value indicates failure.
var channelNum = ssh.OpenSessionChannel();
if (channelNum < 0) {
console.log(ssh.LastErrorText);
return;
}
if (ssh.ChannelIsOpen(channelNum)) {
console.log("The channel is open.");
}
else {
console.log("The channel is not open.");
}
// Send CLOSE, which immediately marks the channel locally closed.
success = ssh.ChannelSendClose(channelNum);
if (success == false) {
console.log(ssh.LastErrorText);
return;
}
if (ssh.ChannelIsOpen(channelNum)) {
console.log("Still open.");
}
else {
console.log("The channel is now closed.");
}
ssh.Disconnect();