Unicode C
Unicode C
SSH Parallel Remote Commands on Multiple Servers
See more SSH Examples
Demonstrates running the same command on several servers simultaneously. It is simply a matter of using one Ssh object per server, starting the command on each with QuickCmdSend, and then polling each connection with QuickCmdCheck until all have finished.
Background: This is the fan-out pattern behind configuration-management and monitoring tools: query or update a whole fleet in roughly the time the slowest single host takes, rather than the sum of all of them. Each server needs its own object because each has its own connection and authentication state. Note that results come back in whatever order the servers finish, which is why the example tracks completion per connection. Production code would keep the objects in arrays instead of repeating the logic per server.
Chilkat Unicode C Downloads
#include <C_CkSshW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSshW ssh1;
HCkSshW ssh2;
HCkSshW ssh3;
int port;
const wchar_t *password;
const wchar_t *cmd;
int channel1;
int channel2;
int channel3;
int pollTimeoutMs;
int numFinished;
BOOL finished1;
BOOL finished2;
BOOL finished3;
int ch1;
const wchar_t *out1;
int ch2;
const wchar_t *out2;
int ch3;
const wchar_t *out3;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates running the same command on several servers simultaneously. It is simply a
// matter of using one Ssh object per server.
ssh1 = CkSshW_Create();
ssh2 = CkSshW_Create();
ssh3 = CkSshW_Create();
port = 22;
// 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_Connect(ssh1,L"ssh-server1.example.com",port);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh1));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
success = CkSshW_AuthenticatePw(ssh1,L"mySshLogin",password);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh1));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
success = CkSshW_Connect(ssh2,L"ssh-server2.example.com",port);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh2));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
success = CkSshW_AuthenticatePw(ssh2,L"mySshLogin",password);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh2));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
success = CkSshW_Connect(ssh3,L"ssh-server3.example.com",port);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh3));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
success = CkSshW_AuthenticatePw(ssh3,L"mySshLogin",password);
if (success == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh3));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
// This command sleeps for 5 seconds and then prints the system date/time, making the parallel
// execution easy to observe.
cmd = L"sleep 5; date";
// Start the command on each server. QuickCmdSend returns immediately.
channel1 = CkSshW_QuickCmdSend(ssh1,cmd);
if (channel1 < 0) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh1));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
channel2 = CkSshW_QuickCmdSend(ssh2,cmd);
if (channel2 < 0) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh2));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
channel3 = CkSshW_QuickCmdSend(ssh3,cmd);
if (channel3 < 0) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh3));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
// The command is now running on all three servers at once. Poll each connection until every
// command has finished. (Production code would keep the objects in arrays rather than
// repeating the logic.)
pollTimeoutMs = 50;
numFinished = 0;
finished1 = FALSE;
finished2 = FALSE;
finished3 = FALSE;
while (numFinished < 3) {
if (!finished1) {
ch1 = CkSshW_QuickCmdCheck(ssh1,pollTimeoutMs);
if (ch1 == -2) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh1));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
if (ch1 >= 0) {
wprintf(L"---- ssh1 channel %d finished ----\n",ch1);
out1 = CkSshW_getReceivedText(ssh1,ch1,L"utf-8");
if (CkSshW_getLastMethodSuccess(ssh1) == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh1));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
wprintf(L"%s\n",out1);
finished1 = TRUE;
numFinished = numFinished + 1;
}
}
if (!finished2) {
ch2 = CkSshW_QuickCmdCheck(ssh2,pollTimeoutMs);
if (ch2 == -2) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh2));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
if (ch2 >= 0) {
wprintf(L"---- ssh2 channel %d finished ----\n",ch2);
out2 = CkSshW_getReceivedText(ssh2,ch2,L"utf-8");
if (CkSshW_getLastMethodSuccess(ssh2) == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh2));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
wprintf(L"%s\n",out2);
finished2 = TRUE;
numFinished = numFinished + 1;
}
}
if (!finished3) {
ch3 = CkSshW_QuickCmdCheck(ssh3,pollTimeoutMs);
if (ch3 == -2) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh3));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
if (ch3 >= 0) {
wprintf(L"---- ssh3 channel %d finished ----\n",ch3);
out3 = CkSshW_getReceivedText(ssh3,ch3,L"utf-8");
if (CkSshW_getLastMethodSuccess(ssh3) == FALSE) {
wprintf(L"%s\n",CkSshW_lastErrorText(ssh3));
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
return;
}
wprintf(L"%s\n",out3);
finished3 = TRUE;
numFinished = numFinished + 1;
}
}
}
CkSshW_Disconnect(ssh1);
CkSshW_Disconnect(ssh2);
CkSshW_Disconnect(ssh3);
CkSshW_Dispose(ssh1);
CkSshW_Dispose(ssh2);
CkSshW_Dispose(ssh3);
}