I am using SSH to communicate with a condor server and need to call commands for custom control (i.e. condor_submit
, condor_make
, condor_q
, etc.). Having downloaded and successfully integrated libSSH in my Xcode project (yes, I'm using Mac OS), I found that the functions provided do not support custom commands. The tutorial stated that this will execute commands on the host:
rc = ssh_channel_request_exec(channel, "ls -l");
if (rc != SSH_OK) {
ssh_channel_close(channel);
ssh_channel_free(channel);
return rc;
}
Yet when I replace the "ls -l"
with let's say "condor_q"
, the command doesn't seem to execute. I managed to fix this by using an interactive shell session like so:
// Create channel
rc = ssh_channel_request_pty(channel);
if (rc != SSH_OK) return rc;
rc = ssh_channel_change_pty_size(channel, 84, 20);
if (rc != SSH_OK) return rc;
rc = ssh_channel_request_shell(channel);
std::string commandString = "condor_q";
char buffer[512];
int bytesRead, bytesWrittenToConsole;
std::string string;
while (ssh_channel_is_open(channel) && !ssh_channel_is_eof(channel)) {
// _nonblocking
bytesRead = ssh_channel_read_nonblocking(channel, buffer, sizeof(buffer), 0);
if (bytesRead < 0) {
rc = SSH_ERROR;
break;
}
if (bytesRead > 0) {
for (int i = 0; i < bytesRead; i++) {
string.push_back(buffer[i]);
}
bytesWrittenToConsole = write(1, buffer, bytesRead);
if (string.find("$") != std::string::npos) {
if (commandString.length() > 0) {
ssh_channel_write(channel, commandString.c_str(), commandString.length());
ssh_channel_write(channel, "\n", 1);
} else {
break;
}
commandString.clear();
string.clear();
}
}
}
// Distroy channel
So my question, is there an easier way to send custom commands via SSH rather than having to "fake-send" the command?
Thanks
Max