将单个文件上传到多机器工具

简介: // 使用示例:// ./mooon_upload -h=192.168.10.11,192.168.10.12 -p=6000 -u=root -P='root123' -s=.

// 使用示例:
// ./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 <fstream>
#include <iostream>

// 逗号分隔的远程主机列表
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::ostream& out, const struct ResultInfo& result)
{
    std::string tag = result.success? "SUCCESS": "FAILURE";
    out << "["PRINT_COLOR_YELLOW << result.ip << PRINT_COLOR_NONE" " << tag << "] " << result.seconds << " seconds";
    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<std::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<struct ResultInfo> results(num_remote_hosts_ip);
    for (int i=0; i<num_remote_hosts_ip; ++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::endl;
    }

    // 输出总结
    std::cout << std::endl;
    std::cout << "================================" << std::endl;
    int num_success = 0; // 成功的个数
    int num_failure = 0; // 失败的个数
    for (std::vector<struct ResultInfo>::size_type i=0; i<results.size(); ++i)
    {
        const struct ResultInfo& result_info = results[i];
        std::cout << result_info << std::endl;

        if (result_info.success)
            ++num_success;
        else
            ++num_failure;
    }
    std::cout << "SUCCESS: " << num_success << ", FAILURE: " << num_failure << std::endl;

    return 0;
}


相关文章
|
3月前
|
自然语言处理 数据挖掘 Linux
ModelScope问题之拷贝到内网linux系统运行代码报错如何解决
本合集将提供ModelScope安装步骤、配置要求和环境准备,以便用户顺利启动ModelScope进行模型开发和测试。
72 0
|
存储 网络协议 Linux
把Linux服务器做成一个下载器,实现远程下载
把Linux服务器做成一个下载器,实现远程下载
把Linux服务器做成一个下载器,实现远程下载
|
1月前
|
前端开发 Java 测试技术
性能工具之 JMeter 上传与下载脚本编写
【4月更文挑战第3天】性能测试工作中,文件上传也是经常见的性能压测场景之一,那么 JMeter 文件上传下载脚本怎么做?
35 2
性能工具之 JMeter 上传与下载脚本编写
|
5月前
|
数据可视化 Linux
【亲测可用】Xshell可视化配置一个登录服务器后自动进入指定目录的方法
【亲测可用】Xshell可视化配置一个登录服务器后自动进入指定目录的方法
【亲测可用】Xshell可视化配置一个登录服务器后自动进入指定目录的方法
|
6月前
|
Java Linux
java实现两台linux服务器间下载上传传输文件
java实现两台linux服务器间下载上传传输文件
|
数据中心
SMAP_SSS_ L2c、L3_V04.0.n 文件下载途径(一键同时下载多个数据~)
最近在分析盐度时,需要用到SMAP数据产品,在浏览网上下载途径时,发现大多是从NASA的数据中心下载的,限于国外网络的网速,表示一言难尽啊、、、 经过不懈努力,找到另一个下载途径,可以下载L2C、L3的SSS、Wind等数据,以及相关产品说明。
SMAP_SSS_ L2c、L3_V04.0.n 文件下载途径(一键同时下载多个数据~)
html+css实战35-上传多个文件功能
html+css实战35-上传多个文件功能
90 0
html+css实战35-上传多个文件功能
|
Java 应用服务中间件
SpringMVC实现文件上传【传统方式上传、跨服务器上传】
SpringMVC实现文件上传【传统方式上传、跨服务器上传】
SpringMVC实现文件上传【传统方式上传、跨服务器上传】
|
网络协议 Linux 数据安全/隐私保护
win10系统下搭建FTP服务器(完成文件上传与下载)
win10系统下搭建FTP服务器(完成文件上传与下载)
562 0
win10系统下搭建FTP服务器(完成文件上传与下载)