Skip to content

Commit dfb6d97

Browse files
committed
Added Remote SSH Support.
1 parent 1046a20 commit dfb6d97

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333

3434
#Extra
3535
.DS_Store
36+
CMakeLists.txt.user
3637

3738
# Final Output
38-
build/ProjectTemplate
39+
build/
40+
third-party/
41+
42+
.cache

source/entrypoint/stl/main.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "fcli.hpp"
2+
#include "remoteexecutor.hpp" // Include the RemoteExecutor module
23

34
/**
45
* @brief Entry point for the FCLI application.
@@ -13,6 +14,35 @@
1314
auto main(int argc, char* argv[]) -> int {
1415
try {
1516
FCLI fcli;
17+
18+
// Check for remote execution flags
19+
if (argc > 1) {
20+
std::string mode = argv[1];
21+
if (mode == "--remote-ssh" && argc == 6) {
22+
std::string user = argv[2];
23+
std::string host = argv[3];
24+
std::string keyPath = argv[4];
25+
std::string command = argv[5];
26+
27+
// Execute remote command via SSH
28+
std::string output = RemoteExecutor::executeSSH(host, user, command, keyPath);
29+
Logger::info("Remote SSH Execution Output:");
30+
Logger::info(output);
31+
return 0; // Success
32+
} else if (mode == "--remote-api" && argc == 5) {
33+
std::string url = argv[2];
34+
std::string token = argv[3];
35+
std::string command = argv[4];
36+
37+
// Execute remote command via API
38+
std::string output = RemoteExecutor::executeAPI(url, command, token);
39+
Logger::info("Remote API Execution Output:");
40+
Logger::info(output);
41+
return 0; // Success
42+
}
43+
}
44+
45+
// Standard CLI execution
1646
fcli.run(argc, argv);
1747
} catch (const std::exception& e) {
1848
Logger::error(std::format("An unexpected error occurred: {}", e.what()));

source/remoteexecutor.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "remoteexecutor.hpp"
2+
#include <iostream>
3+
#include <cstdlib>
4+
#include <sstream>
5+
6+
std::string RemoteExecutor::executeSSH(const std::string& host, const std::string& user,
7+
const std::string& command, const std::string& keyPath) {
8+
std::ostringstream oss;
9+
oss << "ssh -i " << keyPath << " " << user << "@" << host << " \"" << command << "\"";
10+
11+
FILE* pipe = popen(oss.str().c_str(), "r");
12+
if (!pipe) {
13+
throw std::runtime_error("Failed to execute SSH command");
14+
}
15+
16+
char buffer[128];
17+
std::string result;
18+
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
19+
result += buffer;
20+
}
21+
22+
pclose(pipe);
23+
return result;
24+
}
25+
26+
std::string RemoteExecutor::executeAPI(const std::string& url, const std::string& command,
27+
const std::string& token) {
28+
std::ostringstream oss;
29+
oss << "curl -X POST -H \"Authorization: Bearer " << token << "\" "
30+
<< "-d \"command=" << command << "\" " << url;
31+
32+
FILE* pipe = popen(oss.str().c_str(), "r");
33+
if (!pipe) {
34+
throw std::runtime_error("Failed to execute API command");
35+
}
36+
37+
char buffer[128];
38+
std::string result;
39+
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
40+
result += buffer;
41+
}
42+
43+
pclose(pipe);
44+
return result;
45+
}

source/remoteexecutor.hpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef REMOTE_EXECUTOR_HPP
2+
#define REMOTE_EXECUTOR_HPP
3+
4+
#include <string>
5+
#include <stdexcept>
6+
7+
/**
8+
* @class RemoteExecutor
9+
* @brief Provides functionality to execute commands remotely via SSH or API.
10+
*
11+
* This class offers static methods for executing shell commands on remote servers
12+
* using SSH and interacting with APIs to execute commands remotely.
13+
*/
14+
class RemoteExecutor {
15+
public:
16+
/**
17+
* @brief Executes a command on a remote server via SSH.
18+
*
19+
* This method establishes an SSH connection to the specified host using the given
20+
* user credentials and private key. It then executes the provided command remotely
21+
* and captures the output.
22+
*
23+
* @param host The hostname or IP address of the remote server.
24+
* @param user The username for the SSH connection.
25+
* @param command The shell command to execute on the remote server.
26+
* @param keyPath The path to the private key file for SSH authentication.
27+
* @return The output of the command executed on the remote server.
28+
* @throws std::runtime_error If the SSH command execution fails.
29+
*/
30+
static std::string executeSSH(const std::string& host, const std::string& user,
31+
const std::string& command, const std::string& keyPath);
32+
33+
/**
34+
* @brief Executes a command via an API.
35+
*
36+
* This method sends a POST request to the specified API URL with the provided command
37+
* as a parameter. Authentication is performed using a bearer token.
38+
*
39+
* @param url The API endpoint URL.
40+
* @param command The command or data to send to the API for execution.
41+
* @param token The bearer token for API authentication.
42+
* @return The response from the API.
43+
* @throws std::runtime_error If the API request fails.
44+
*/
45+
static std::string executeAPI(const std::string& url, const std::string& command,
46+
const std::string& token);
47+
};
48+
49+
#endif // REMOTE_EXECUTOR_HPP

0 commit comments

Comments
 (0)