Upload a single file to multiple machines tool
Usage example:
./mooon_upload -h=192.168.10.11,192.168.10.12 -p=6000 -u=root -P= 'root123' -s=./abc -d=/tmp/
means to upload the local file ./abc to the /tmp/ directory
#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
// Comma separated list of remote hosts
STRING_ARG_DEFINE(h, "", "remote hosts");-
// Sshd port number of the remote host
INTEGER_ARG_DEFINE(uint16_t, p, 22, 10, 65535, "remote hosts port");
// Username
STRING_ARG_DEFINE(u, "root", "remote host user");
// Password
STRING_ARG_DEFINE(P , "", "remote host password");
// Uploaded file path
STRING_ARG_DEFINE(s, " ", "the source file uploaded");
// Directory path where the file is stored after uploading
STRING_ARG_DEFINE(d, "", "the directory to store" ; timeout seconds to remote host");
// Result information
struct ResultInfo
{
bool success; // true indicates successful execution
std::string ip; // IP address of the remote host
uint32_t seconds; // The time it takes to run, accurate to 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[])
{
// Parse command line parameters
std::string errmsg;
if (!mooon::utils: :parse_arguments(argc, argv, &errmsg))
{
fprintf(stderr, "parameter error: %sn", 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);
// Check parameters (-s)
if (source.empty())
{
fprintf(stderr, "parameter[-s]'s value not setn");-
exit(1);
}
// Check parameters (-d) -
if (directory.empty())
{
fprintf(stderr, "parameter[-d]'s value not setn") ;
exit(1);
}
// Check parameters (-h )
if (hosts.empty())
{
// Try to get the value from the environment variable
const char* hosts_ = getenv("HOSTS");
if (NULL == hosts_)
{
fprintf(stderr, "parameter[-h]'s value not setn");
exit(1);
}
hosts= hosts_;
mooon::utils::CStringUtils::trim(hosts);
if (hosts .empty())
{
fprintf(stderr, "parameter[-h]'s value not setn");
exit(1);
}
}
// Check parameters (- u)
if (user.empty())
{-
- fprintf(stderr, "parameter[-u]'s value not setn");
- exit(1);
- }
- // 检查参数(-P)
- if (password.empty())
- {
- fprintf(stderr, "parameter[-P]'s value not setn");
- exit(1);
- }
- std::vector 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] errorn");
- 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 bytesn", 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: %sn", 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: %sn", 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::size_type i=0; 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;
- }
http://www.bkjia.com/PHPjc/1117250.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1117250.htmlTechArticle上传单个文件到多台机器工具 使用示例: ./mooon_upload -h=192.168.10.11,192.168.10.12 -p=6000 -u=root -P='root123' -s=./abc -d=/tmp/ 表示将本地的文件./abc上传...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn