87 lines
3.1 KiB
C++
87 lines
3.1 KiB
C++
#ifndef EXECUTE_HPP
|
|
#define EXECUTE_HPP
|
|
|
|
#include <string>
|
|
#include <map>
|
|
|
|
namespace dropshell {
|
|
|
|
class sCommand;
|
|
|
|
// mode bitset
|
|
enum class cMode {
|
|
Defaults = 0,
|
|
Interactive = 1,
|
|
Silent = 2,
|
|
NoBB64 = 4
|
|
};
|
|
|
|
inline cMode operator&(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) & static_cast<int>(rhs));}
|
|
inline cMode operator+(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) | static_cast<int>(rhs));}
|
|
inline cMode operator-(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) & ~static_cast<int>(rhs));}
|
|
inline cMode operator|(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) | static_cast<int>(rhs));}
|
|
inline cMode operator|=(cMode & lhs, cMode rhs) {return lhs = lhs | rhs;}
|
|
inline bool hasFlag(cMode mode, cMode flag) {return (mode & flag) == flag;}
|
|
|
|
|
|
class sSSHInfo {
|
|
public:
|
|
sSSHInfo(std::string host, std::string user, std::string port, std::string server_ID, std::string user_dir) :
|
|
host(host), user(user), port(port), server_ID(server_ID), user_dir(user_dir) {}
|
|
|
|
std::string get_host() const { return host; }
|
|
std::string get_user() const { return user; }
|
|
std::string get_port() const { return port; }
|
|
std::string get_server_ID() const { return server_ID; }
|
|
std::string get_user_dir() const { return user_dir; }
|
|
|
|
bool valid() const;
|
|
private:
|
|
std::string host;
|
|
std::string user;
|
|
std::string port;
|
|
std::string server_ID; // dropshell name for server.
|
|
std::string user_dir; // dropshell directory for the user.
|
|
};
|
|
|
|
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> & env_vars, std::string * output = nullptr, cMode mode = cMode::Defaults);
|
|
bool execute_ssh_command(const sSSHInfo & ssh_info, const sCommand & remote_command, cMode mode = cMode::Defaults, std::string * output = nullptr);
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
// class to hold a command to run on the remote server.
|
|
class sCommand {
|
|
public:
|
|
sCommand(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> & env_vars) :
|
|
mDir(directory_to_run_in), mCmd(command_to_run), mVars(env_vars) {}
|
|
|
|
std::string get_directory_to_run_in() const { return mDir; }
|
|
std::string get_command_to_run() const { return mCmd; }
|
|
const std::map<std::string, std::string>& get_env_vars() const { return mVars; }
|
|
|
|
void add_env_var(const std::string& key, const std::string& value) { mVars[key] = value; }
|
|
|
|
bool empty() const { return mCmd.empty(); }
|
|
|
|
std::string construct_cmd(std::string bb64path) const;
|
|
|
|
private:
|
|
std::string makesafecmd(std::string bb64path, const std::string& command) const;
|
|
|
|
private:
|
|
std::string mDir;
|
|
std::string mCmd;
|
|
std::map<std::string, std::string> mVars;
|
|
};
|
|
|
|
bool EXITSTATUSCHECK(int ret);
|
|
|
|
|
|
} // namespace dropshell
|
|
|
|
|
|
|
|
#endif
|