如何从c++代码在另一台计算机上运行程序
How to run a program on another computer from c++ code?
我想让我的c++程序在PC1上启动PC2上的另一个程序,给定PC2的主机名。我现在不想把事情弄得太复杂,所以让我们假设这个程序在PC2上的可执行文件的搜索路径上。据我所知,这可以通过ssh完成吗?假设(为了进一步简化)我在PC1和PC2上都有一个帐户,这样当我在PC1上登录时,ssh就会连接我(没有任何需要我提供用户名和密码的交互),我该怎么做呢?https://www.libssh.org/是否有助于简化事情?
您可能对这个c++ RPC库感兴趣:
http://szelei.me/introducing-rpclib从自己的例子中,在远程计算机上:
#include <iostream>
#include "rpc/server.h"
void foo() {
std::cout << "foo was called!" << std::endl;
}
int main(int argc, char *argv[]) {
// Creating a server that listens on port 8080
rpc::server srv(8080);
// Binding the name "foo" to free function foo.
// note: the signature is automatically captured
srv.bind("foo", &foo);
// Binding a lambda function to the name "add".
srv.bind("add", [](int a, int b) {
return a + b;
});
// Run the server loop.
srv.run();
return 0;
}
在本地:
#include <iostream>
#include "rpc/client.h"
int main() {
// Creating a client that connects to the localhost on port 8080
rpc::client client("127.0.0.1", 8080);
// Calling a function with paramters and converting the result to int
auto result = client.call("add", 2, 3).as<int>();
std::cout << "The result is: " << result << std::endl;
return 0;
}
要执行任何事情,你可以有一个"系统";在远程计算机上调用。所以在服务器端输入:
// Binding a lambda function to the name "system".
srv.bind("system", [](char const * command) {
return system(command);
});
现在在客户端你做:
auto result = client.call("system", "ls").as<int>();
显然,如果您想使用这样的库,您需要考虑安全性。这将在可信的局域网环境中很好地工作。在Internet这样的公共网络中,这可能不是一个好主意。
构造一个命令行,使用ssh
执行远程命令。然后使用system()
执行该命令。
std::string pc2_hostname;
std::string cmd = "ssh " + pc2_hostname + " command_to_execute";
system(cmd.c_str());
相关文章:
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- 在另一台 PC 上执行程序时加载 SDL2 共享库时出错
- 另一台电脑上的Visual Studio C++项目无法生成
- Homebrew OS X OpenCV - 在另一台Mac上运行时寻找dylib
- 当我在支持 avx2 的机器上编译并在另一台仅支持 avx 的机器上运行二进制文件时会发生什么?
- 如何在另一台未安装 Visual Studio 的计算机中使用由 Visual Studio 生成的 dll
- 在另一台计算机中运行 GTKMM 代码时未定义的符号
- 将Visual Studio C 构建的中间文件移至另一台计算机
- 为什么我的Glreadpixels程序在另一台计算机中不起作用
- 在另一台计算机MFC C++上不进入while循环
- 我可以将TokenPrivileges数组从一台计算机复制到另一台计算机吗
- 从另一台计算机运行opencv可执行文件
- 通过网络直接从另一台计算机的内存中读取文件
- fwrite在一台计算机上浮动,在另一台计算机中浮动
- 在C++中使用 zeromq 将序列化的 Mat 对象发送到另一台计算机
- 一台计算机中出现 msvs 2013 错误 C2057,另一台计算机中没有错误。这是怎么回事?
- 使用蓝牙将陀螺仪数据传输到另一台计算机
- 使用脉冲编码调制从一台计算机到另一台计算机的数据传输
- 如何在另一台计算机(linux)上使用OpenCV运行c++库
- 在局域网上选择一台计算机作为服务器