This commit is contained in:
parent
34f2763ef4
commit
de337f51f3
@ -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<std::string, std::string>& 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);
|
||||
|
@ -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<std::string> args = {};
|
||||
std::string working_dir = "/home";
|
||||
std::map<std::string, std::string> 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();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user