diff --git a/runner/runner.cpp b/runner/runner.cpp index 355500b..d9853ce 100644 --- a/runner/runner.cpp +++ b/runner/runner.cpp @@ -123,12 +123,16 @@ int ssh_interactive_shell_session(ssh_session session, ssh_channel channel, cons } int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string& remote_cmd_str, bool silent, std::string* output, const std::map& env) { - // Set environment variables using ssh_channel_request_env + // Build command with env assignments as prefix using 'env' + std::ostringstream cmd_with_env; + cmd_with_env << "env "; for (const auto& kv : env) { if (kv.first == "SSHPASS") continue; - ssh_channel_request_env(channel, kv.first.c_str(), kv.second.c_str()); + cmd_with_env << kv.first << "='" << kv.second << "' "; } - int rc = ssh_channel_request_exec(channel, remote_cmd_str.c_str()); + cmd_with_env << remote_cmd_str; + std::string final_cmd = cmd_with_env.str(); + int rc = ssh_channel_request_exec(channel, final_cmd.c_str()); if (rc != SSH_OK) { if (output) *output = std::string("Failed to exec remote command: ") + ssh_get_error(session); return -1; @@ -260,7 +264,7 @@ int execute_cmd( ssh_free(session); return -1; } - std::string remote_cmd_str = ssh_build_remote_command(command, args, working_dir, {}); // Don't prefix env + std::string remote_cmd_str = ssh_build_remote_command(command, args, working_dir, {}); // Don't prefix env here int ret = 0; if (interactive) { ret = ssh_interactive_shell_session(session, channel, remote_cmd_str, command, output); diff --git a/runner/runner_demo.cpp b/runner/runner_demo.cpp index 7b0e016..50b5cf6 100644 --- a/runner/runner_demo.cpp +++ b/runner/runner_demo.cpp @@ -92,9 +92,23 @@ void test_ssh() { runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh); } +void test_env() { + std::string command = "env"; + std::vector args = {}; + std::string working_dir = "/home"; + std::map env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}}; + bool silent = false; + bool interactive = false; + runner::sSSHInfo ssh; + ssh.host = "10.10.10.13"; + ssh.user = "katie"; + ssh.port = "22"; -int main(int argc, char* argv[]) { - test_ssh(); - + runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh); +} + + +int main(int argc, char* argv[]) { + test_env(); }