This commit is contained in:
parent
5973d63d3e
commit
34f2763ef4
@ -122,7 +122,12 @@ int ssh_interactive_shell_session(ssh_session session, ssh_channel channel, cons
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string& remote_cmd_str, bool silent, std::string* output) {
|
||||
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
|
||||
for (const auto& kv : env) {
|
||||
if (kv.first == "SSHPASS") continue;
|
||||
ssh_channel_request_env(channel, kv.first.c_str(), kv.second.c_str());
|
||||
}
|
||||
int rc = ssh_channel_request_exec(channel, remote_cmd_str.c_str());
|
||||
if (rc != SSH_OK) {
|
||||
if (output) *output = std::string("Failed to exec remote command: ") + ssh_get_error(session);
|
||||
@ -255,12 +260,12 @@ int execute_cmd(
|
||||
ssh_free(session);
|
||||
return -1;
|
||||
}
|
||||
std::string remote_cmd_str = ssh_build_remote_command(command, args, working_dir, env);
|
||||
std::string remote_cmd_str = ssh_build_remote_command(command, args, working_dir, {}); // Don't prefix env
|
||||
int ret = 0;
|
||||
if (interactive) {
|
||||
ret = ssh_interactive_shell_session(session, channel, remote_cmd_str, command, output);
|
||||
} else {
|
||||
ret = ssh_exec_command(session, channel, remote_cmd_str, silent, output);
|
||||
ret = ssh_exec_command(session, channel, remote_cmd_str, silent, output, env);
|
||||
}
|
||||
ssh_channel_send_eof(channel);
|
||||
ssh_channel_close(channel);
|
||||
|
@ -79,7 +79,7 @@ void test_docker() {
|
||||
|
||||
void test_ssh() {
|
||||
std::string command = "bash";
|
||||
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::map<std::string, std::string> env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}};
|
||||
bool silent = false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user