This commit is contained in:
@ -9,11 +9,38 @@
|
||||
#include <libssh/callbacks.h>
|
||||
#include <termios.h>
|
||||
#include <sys/select.h>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
namespace runner {
|
||||
|
||||
namespace {
|
||||
|
||||
// String trimming functions
|
||||
void ltrim(std::string& s) {
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
}
|
||||
|
||||
void rtrim(std::string& s) {
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), s.end());
|
||||
}
|
||||
|
||||
void trim(std::string& s) {
|
||||
ltrim(s);
|
||||
rtrim(s);
|
||||
}
|
||||
|
||||
// Safe version that handles nullptr
|
||||
void trim(std::string* s) {
|
||||
if (s) {
|
||||
trim(*s);
|
||||
}
|
||||
}
|
||||
|
||||
// Add working_dir to the forward declaration
|
||||
ssh_session ssh_connect_and_auth(const sSSHInfo* sshinfo, const std::map<std::string, std::string>& env, std::string* error);
|
||||
std::string ssh_build_remote_command(const std::string& command, const std::vector<std::string>& args, const std::string& working_dir, const std::map<std::string, std::string>& env);
|
||||
@ -192,9 +219,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string
|
||||
|
||||
std::string final_cmd = cmd_with_env.str();
|
||||
|
||||
// Debug: Show the command being executed
|
||||
std::cerr << "SSH exec command: " << final_cmd << std::endl;
|
||||
|
||||
int rc = ssh_channel_request_exec(channel, final_cmd.c_str());
|
||||
if (rc != SSH_OK) {
|
||||
std::string error = std::string("Failed to exec remote command: ") + ssh_get_error(session);
|
||||
@ -210,7 +234,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string
|
||||
|
||||
// Read from stdout
|
||||
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
|
||||
std::cerr << "Read " << nbytes << " bytes from stdout" << std::endl;
|
||||
oss.write(buffer, nbytes);
|
||||
}
|
||||
if (nbytes < 0) {
|
||||
@ -219,7 +242,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string
|
||||
|
||||
// Read from stderr
|
||||
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 1)) > 0) {
|
||||
std::cerr << "Read " << nbytes << " bytes from stderr" << std::endl;
|
||||
oss.write(buffer, nbytes);
|
||||
}
|
||||
if (nbytes < 0) {
|
||||
@ -233,7 +255,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string
|
||||
|
||||
// Read from stdout
|
||||
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
|
||||
std::cerr << "Read " << nbytes << " bytes from stdout (writing to fd 1)" << std::endl;
|
||||
write(1, buffer, nbytes);
|
||||
}
|
||||
if (nbytes < 0) {
|
||||
@ -242,7 +263,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string
|
||||
|
||||
// Read from stderr
|
||||
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 1)) > 0) {
|
||||
std::cerr << "Read " << nbytes << " bytes from stderr (writing to fd 2)" << std::endl;
|
||||
write(2, buffer, nbytes);
|
||||
}
|
||||
if (nbytes < 0) {
|
||||
@ -378,9 +398,12 @@ int execute_cmd(
|
||||
ssh_channel_free(channel);
|
||||
ssh_disconnect(session);
|
||||
ssh_free(session);
|
||||
if (output) trim(output);
|
||||
return ret;
|
||||
} else {
|
||||
return local_execute_cmd(command, args, working_dir, env, silent, interactive, output);
|
||||
int ret=local_execute_cmd(command, args, working_dir, env, silent, interactive, output);
|
||||
if (output) trim(output);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user