Unicode C
Unicode C
Start a Remote Shell with QuickShell
See more SSH Examples
Demonstrates the Chilkat Ssh.QuickShell method, which starts a remote shell using the simplified sequence OpenSessionChannel → SendReqPty → SendReqShell. It takes no arguments and returns the shell channel number, or -1 on failure. The default PTY size is 80 columns by 24 rows.
Background: Note that
QuickShell does allocate a PTY, so unlike a bare SendReqShell the remote shell runs interactively: it prints a command prompt, echoes the commands you send, and runs interactive login scripts. That is convenient for terminal-style work but means the output contains prompts and echoed input that scripted parsing must tolerate. The method returns as soon as the shell request is accepted — it does not consume the initial login text or prompt, so read and discard that before relying on command output.Chilkat Unicode C Downloads
#include <C_CkSshW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSshW ssh;
int sshPort;
const wchar_t *password;
int channelNum;
BOOL caseSensitive;
const wchar_t *output;
success = FALSE;
// Demonstrates the Ssh.QuickShell method, which starts a remote shell using the simplified
// sequence OpenSessionChannel, SendReqPty, and SendReqShell. It takes no arguments and returns
// the shell channel number, or -1 on failure. The default PTY size is 80 columns by 24 rows.
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;
}
// Start the shell. Note that QuickShell allocates a PTY, so the remote shell runs
// interactively: it prints a command prompt and echoes the commands that are sent to it.
channelNum = CkSshW_QuickShell(ssh);
if (channelNum < 0) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
CkSshW_Dispose(ssh);
return;
}
// QuickShell returns as soon as the shell request is accepted -- it does not wait for or
// consume the initial login text or prompt.
success = CkSshW_ChannelSendString(ssh,channelNum,L"uname -a; echo DONE_MARKER\n",L"utf-8");
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
CkSshW_Dispose(ssh);
return;
}
// IMPORTANT: Set a read timeout before receiving until a match. ReadTimeoutMs defaults to 0,
// which means no limit -- without it, this call waits forever if the received data never
// contains a match.
CkSshW_putReadTimeoutMs(ssh,15000);
caseSensitive = FALSE;
success = CkSshW_ChannelReceiveUntilMatch(ssh,channelNum,L"DONE_MARKER",L"utf-8",caseSensitive);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
CkSshW_Dispose(ssh);
return;
}
output = CkSshW_getReceivedText(ssh,channelNum,L"utf-8");
if (CkSshW_getLastMethodSuccess(ssh) == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
CkSshW_Dispose(ssh);
return;
}
wprintf(L"%s\n",output);
CkSshW_Disconnect(ssh);
CkSshW_Dispose(ssh);
}