上传单个文件到多台机器工具_PHP教程
上传单个文件到多台机器工具
使用示例:./mooon_upload -h=192.168.10.11,192.168.10.12 -p=6000 -u=root -P='root123' -s=./abc -d=/tmp/
表示将本地的文件./abc上传到两台机器192.168.10.11和192.168.10.12的/tmp/目录
- #include "mooon/net/libssh2.h"
- #include "mooon/sys/stop_watch.h"
- #include "mooon/utils/args_parser.h"
- #include "mooon/utils/print_color.h"
- #include "mooon/utils/string_utils.h"
- #include "mooon/utils/tokener.h"
- #include
- #include
- // 逗号分隔的远程主机列表
- STRING_ARG_DEFINE(h, "", "remote hosts");
- // 远程主机的sshd端口号
- INTEGER_ARG_DEFINE(uint16_t, p, 22, 10, 65535, "remote hosts port");
- // 用户名
- STRING_ARG_DEFINE(u, "root", "remote host user");
- // 密码
- STRING_ARG_DEFINE(P, "", "remote host password");
- // 被上传的文件路径
- STRING_ARG_DEFINE(s, "", "the source file uploaded");
- // 文件上传后存放的目录路径
- STRING_ARG_DEFINE(d, "", "the directory to store");
- // 连接超时,单位为秒
- INTEGER_ARG_DEFINE(uint16_t, t, 10, 1, 65535, "timeout seconds to remote host");
- // 结果信息
- struct ResultInfo
- {
- bool success; // 为true表示执行成功
- std::string ip; // 远程host的IP地址
- uint32_t seconds; // 运行花费的时长,精确到秒
- ResultInfo()
- : success(false), seconds(0)
- {
- }
- std::string str() const
- {
- std::string tag = success? "SUCCESS": "FAILURE";
- return mooon::utils::CStringUtils::format_string("[%s %s]: %u seconds", ip.c_str(), tag.c_str(), seconds);
- }
- };
- inline std::ostream& operator
- {
- std::string tag = result.success? "SUCCESS": "FAILURE";
- out
- return out;
- }
- int main(int argc, char* argv[])
- {
- // 解析命令行参数
- std::string errmsg;
- if (!mooon::utils::parse_arguments(argc, argv, &errmsg))
- {
- fprintf(stderr, "parameter error: %s\n", errmsg.c_str());
- exit(1);
- }
- uint16_t port = mooon::argument::p->value();
- std::string source = mooon::argument::s->value();
- std::string directory = mooon::argument::d->value();
- std::string hosts = mooon::argument::h->value();
- std::string user = mooon::argument::u->value();
- std::string password = mooon::argument::P->value();
- mooon::utils::CStringUtils::trim(source);
- mooon::utils::CStringUtils::trim(directory);
- mooon::utils::CStringUtils::trim(hosts);
- mooon::utils::CStringUtils::trim(user);
- mooon::utils::CStringUtils::trim(password);
- // 检查参数(-s)
- if (source.empty())
- {
- fprintf(stderr, "parameter[-s]'s value not set\n");
- exit(1);
- }
- // 检查参数(-d)
- if (directory.empty())
- {
- fprintf(stderr, "parameter[-d]'s value not set\n");
- exit(1);
- }
- // 检查参数(-h)
- if (hosts.empty())
- {
- // 尝试从环境变量取值
- const char* hosts_ = getenv("HOSTS");
- if (NULL == hosts_)
- {
- fprintf(stderr, "parameter[-h]'s value not set\n");
- exit(1);
- }
- hosts= hosts_;
- mooon::utils::CStringUtils::trim(hosts);
- if (hosts.empty())
- {
- fprintf(stderr, "parameter[-h]'s value not set\n");
- exit(1);
- }
- }
- // 检查参数(-u)
- if (user.empty())
- {
- fprintf(stderr, "parameter[-u]'s value not set\n");
- exit(1);
- }
- // 检查参数(-P)
- if (password.empty())
- {
- fprintf(stderr, "parameter[-P]'s value not set\n");
- exit(1);
- }
- std::vector<:string> hosts_ip;
- const std::string& remote_hosts_ip = hosts;
- int num_remote_hosts_ip = mooon::utils::CTokener::split(&hosts_ip, remote_hosts_ip, ",", true);
- if (0 == num_remote_hosts_ip)
- {
- fprintf(stderr, "parameter[-h] error\n");
- exit(1);
- }
- std::string remote_filepath = directory + std::string("/") + mooon::utils::CStringUtils::extract_filename(source);
- std::vector
results(num_remote_hosts_ip); - for (int i=0; i
- {
- bool color = true;
- const std::string& remote_host_ip = hosts_ip[i];
- results[i].ip = remote_host_ip;
- results[i].success = false;
- fprintf(stdout, "["PRINT_COLOR_YELLOW"%s"PRINT_COLOR_NONE"]\n", remote_host_ip.c_str());
- fprintf(stdout, PRINT_COLOR_GREEN);
- mooon::sys::CStopWatch stop_watch;
- try
- {
- int file_size = 0;
- mooon::net::CLibssh2 libssh2(remote_host_ip, port, user, password, mooon::argument::t->value());
- libssh2.upload(source, remote_filepath, &file_size);
- fprintf(stdout, "["PRINT_COLOR_YELLOW"%s"PRINT_COLOR_NONE"] SUCCESS: %d bytes\n", remote_host_ip.c_str(), file_size);
- results[i].success = true;
- }
- catch (mooon::sys::CSyscallException& ex)
- {
- if (color)
- fprintf(stdout, PRINT_COLOR_NONE); // color = true;
- fprintf(stderr, "["PRINT_COLOR_RED"%s"PRINT_COLOR_NONE"] failed: %s\n", remote_host_ip.c_str(), ex.str().c_str());
- }
- catch (mooon::utils::CException& ex)
- {
- if (color)
- fprintf(stdout, PRINT_COLOR_NONE); // color = true;
- fprintf(stderr, "["PRINT_COLOR_RED"%s"PRINT_COLOR_NONE"] failed: %s\n", remote_host_ip.c_str(), ex.str().c_str());
- }
- results[i].seconds = stop_watch.get_elapsed_microseconds() / 1000000;
- std::cout
- }
- // 输出总结
- std::cout
- std::cout
- int num_success = 0; // 成功的个数
- int num_failure = 0; // 失败的个数
- for (std::vector
::size_type i=0; i - {
- const struct ResultInfo& result_info = results[i];
- std::cout
- if (result_info.success)
- ++num_success;
- else
- ++num_failure;
- }
- std::cout
- return 0;
- }

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

OnLeaks 现在与 Android Headlines 合作,首次展示了 Galaxy S25 Ultra,几天前,他试图从他的 X(以前的 Twitter)粉丝那里筹集到 4,000 美元以上的资金,但失败了。对于上下文,嵌入在 h 下面的渲染图像

除了发布两款新智能手机外,TCL 还发布了一款名为 NXTPAPER 14 的新 Android 平板电脑,其大屏幕尺寸是其卖点之一。 NXTPAPER 14 采用 TCL 标志性品牌哑光液晶面板 3.0 版本

Vivo Y300 Pro刚刚全面亮相,它是最薄的中端Android手机之一,配备大电池。准确来说,这款智能手机的厚度仅为 7.69 毫米,但配备了 6,500 mAh 的电池。这与最近推出的容量相同

三星尚未就何时更新其 Fan Edition (FE) 智能手机系列提供任何提示。目前来看,Galaxy S23 FE 仍然是该公司的最新版本,于 2023 年 10 月年初推出。

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

Redmi Note 14 Pro Plus 现已正式成为去年 Redmi Note 13 Pro Plus 的直接后继产品(亚马逊售价 375 美元)。正如预期的那样,Redmi Note 14 Pro Plus与Redmi Note 14和Redmi Note 14 Pro一起成为Redmi Note 14系列的主角。李

摩托罗拉今年发布了无数设备,尽管其中只有两款是可折叠的。就上下文而言,虽然世界上大多数地区都收到了 Razr 50 和 Razr 50 Ultra,但摩托罗拉在北美提供了 Razr 2024 和 Razr 2
