I am using execv to set environment variable by passing command line argument. how to set environment variable without passing command line argument.
std::vector<char*> arg;
arg.push_back("export MONGODB_HOSTNAME=" + mongodb_host + ";");
arg.push_back("export MONGODB_USERNAME=" + mongo_username + ";");
arg.push_back("export MONGODB_PASSWORD=" + mongo_passowrd + ";");
arg.push_back("NULL");
execv("/usr/bin/ssh", &arg.front());
Depending on your threat model, perhaps you can get away with setting environment variables via /usr/bin/env
?
This would look something like the following:
std::vector<std::string> strArgs = {
"ssh",
"remote-server",
"env",
"MONGODB_USERNAME=" + mongo_username,
"MONGODB_PASSWORD=" + mongo_password,
"mongo"
};
// Convert std::vector<std::string> into char**
std::vector<char*> cArgs;
for (auto& str : strArgs) {
cArgs.emplace_back(str.c_str());
}
execv("/usr/bin/ssh", (const char **)&cArgs.front());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.