Unicode C
Unicode C
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 Unicode C Downloads
#include <C_CkSshW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSshW ssh;
int sshPort;
const wchar_t *password;
int channelNum;
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.
ssh = CkSshW_Create();
sshPort = 22;
success = CkSshW_Connect(ssh,L"ssh.example.com",sshPort);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
CkSshW_Dispose(ssh);
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.
password = L"mySshPassword";
success = CkSshW_AuthenticatePw(ssh,L"mySshLogin",password);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
CkSshW_Dispose(ssh);
return;
}
// Open a session channel. A negative return value indicates failure.
channelNum = CkSshW_OpenSessionChannel(ssh);
if (channelNum < 0) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
CkSshW_Dispose(ssh);
return;
}
if (CkSshW_ChannelIsOpen(ssh,channelNum)) {
wprintf(L"The channel is open.\n");
}
else {
wprintf(L"The channel is not open.\n");
}
// Send CLOSE, which immediately marks the channel locally closed.
success = CkSshW_ChannelSendClose(ssh,channelNum);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
CkSshW_Dispose(ssh);
return;
}
if (CkSshW_ChannelIsOpen(ssh,channelNum)) {
wprintf(L"Still open.\n");
}
else {
wprintf(L"The channel is now closed.\n");
}
CkSshW_Disconnect(ssh);
CkSshW_Dispose(ssh);
}