Seems to work
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 21s

This commit is contained in:
Your Name 2025-05-10 14:43:35 +12:00
parent 409f532409
commit ec5f4ad38d
2 changed files with 49 additions and 24 deletions

View File

@ -9,11 +9,38 @@
#include <libssh/callbacks.h> #include <libssh/callbacks.h>
#include <termios.h> #include <termios.h>
#include <sys/select.h> #include <sys/select.h>
#include <algorithm>
#include <cctype>
namespace runner { namespace runner {
namespace { 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 // 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); 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); 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(); 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()); int rc = ssh_channel_request_exec(channel, final_cmd.c_str());
if (rc != SSH_OK) { if (rc != SSH_OK) {
std::string error = std::string("Failed to exec remote command: ") + ssh_get_error(session); 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 // Read from stdout
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) { while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
std::cerr << "Read " << nbytes << " bytes from stdout" << std::endl;
oss.write(buffer, nbytes); oss.write(buffer, nbytes);
} }
if (nbytes < 0) { if (nbytes < 0) {
@ -219,7 +242,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string
// Read from stderr // Read from stderr
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 1)) > 0) { while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 1)) > 0) {
std::cerr << "Read " << nbytes << " bytes from stderr" << std::endl;
oss.write(buffer, nbytes); oss.write(buffer, nbytes);
} }
if (nbytes < 0) { if (nbytes < 0) {
@ -233,7 +255,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string
// Read from stdout // Read from stdout
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) { 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); write(1, buffer, nbytes);
} }
if (nbytes < 0) { if (nbytes < 0) {
@ -242,7 +263,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string
// Read from stderr // Read from stderr
while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 1)) > 0) { 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); write(2, buffer, nbytes);
} }
if (nbytes < 0) { if (nbytes < 0) {
@ -378,9 +398,12 @@ int execute_cmd(
ssh_channel_free(channel); ssh_channel_free(channel);
ssh_disconnect(session); ssh_disconnect(session);
ssh_free(session); ssh_free(session);
if (output) trim(output);
return ret; return ret;
} else { } 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;
} }
} }

View File

@ -11,6 +11,16 @@
#include <openssl/bio.h> #include <openssl/bio.h>
#include <openssl/evp.h> #include <openssl/evp.h>
// Simple testing framework
#define TESTRESULT(condition, description) \
do { \
std::cout << (condition ? "\033[32m[PASS]\033[0m " : "\033[31m[FAIL]\033[0m ") << description; \
if (!(condition)) { \
std::cout << " - Expected condition to be true"; \
} \
std::cout << std::endl; \
} while(0)
// using json = nlohmann::json; // using json = nlohmann::json;
// std::string base64_decode(const std::string& encoded) { // std::string base64_decode(const std::string& encoded) {
@ -82,20 +92,22 @@ void test_ssh() {
std::vector<std::string> args = {"-c", "ls -l && echo \"$WHATSUP\""}; std::vector<std::string> args = {"-c", "ls -l && echo \"$WHATSUP\""};
std::string working_dir = "/home"; std::string working_dir = "/home";
std::map<std::string, std::string> env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}}; std::map<std::string, std::string> env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}};
bool silent = false; bool silent = true;
bool interactive = false; bool interactive = false;
runner::sSSHInfo ssh; runner::sSSHInfo ssh;
ssh.host = "10.10.10.13"; ssh.host = "10.10.10.13";
ssh.user = "katie"; ssh.user = "katie";
ssh.port = "22"; ssh.port = "22";
runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh); std::string output;
int ret = runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh, &output);
TESTRESULT(ret == 0, "SSH test (ls and echo).");
} }
void test_env() { void test_env() {
std::string command = "bash"; std::string command = "bash";
// Simplify the command to avoid nested quoting issues // Simplify the command to avoid nested quoting issues
std::vector<std::string> args = {"-c", "env | grep WHATSUP && echo The value is: $WHATSUP"}; std::vector<std::string> args = {"-c", "echo $WHATSUP"};
std::string working_dir = "/home"; std::string working_dir = "/home";
std::map<std::string, std::string> env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}}; std::map<std::string, std::string> env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}};
bool silent = false; bool silent = false;
@ -104,27 +116,17 @@ void test_env() {
ssh.host = "10.10.10.13"; ssh.host = "10.10.10.13";
ssh.user = "katie"; ssh.user = "katie";
ssh.port = "22"; ssh.port = "22";
std::cerr << "Running test_env with SSH to " << ssh.user << "@" << ssh.host << std::endl;
// Capture output explicitly // Capture output explicitly
std::string output; std::string output;
int ret = runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh, &output); int ret = runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh, &output);
std::cerr << "Command returned code: " << ret << std::endl; TESTRESULT(output == "Waaaaattttsssuuuppppp!","Whatsupppp test (remote env).");
std::cerr << "Output length: " << output.length() << " bytes" << std::endl;
// Print the actual output
std::cout << "=== BEGIN OUTPUT ===" << std::endl;
std::cout << output;
if (!output.empty() && output[output.length()-1] != '\n') {
std::cout << std::endl;
}
std::cout << "=== END OUTPUT ===" << std::endl;
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
test_ssh();
test_env(); test_env();
} }