Upload a single file to multiple machines tool_PHP tutorial

WBOY
Release: 2016-07-12 08:55:08
Original
903 people have browsed it

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"

  1. #include "mooon/sys/stop_watch.h"

  2. #include "mooon/ utils/args_parser.h"

  3. #include "mooon/utils/print_color.h"

  4. #include "mooon/utils/string_utils.h"

  5. #include "mooon/utils/tokener.h"

  6. #include

  7. #include


  8. // Comma separated list of remote hosts

  9. STRING_ARG_DEFINE(h, "", "remote hosts");

  10. // Sshd port number of the remote host

  11. INTEGER_ARG_DEFINE(uint16_t, p, 22, 10, 65535, "remote hosts port");

  12. // Username

  13. STRING_ARG_DEFINE(u, "root", "remote host user");

  14. // Password

  15. STRING_ARG_DEFINE(P , "", "remote host password");


  16. // Uploaded file path

  17. STRING_ARG_DEFINE(s, " ", "the source file uploaded");

  18. // Directory path where the file is stored after uploading

  19. STRING_ARG_DEFINE(d, "", "the directory to store" ; timeout seconds to remote host");


  20. // Result information

  21. struct ResultInfo

  22. {

  23. bool success; // true indicates successful execution

  24. std::string ip; // IP address of the remote host

  25. uint32_t seconds; // The time it takes to run, accurate to seconds


  26. ResultInfo()

  27. : success(false), seconds(0)

  28. {

  29. }


  30. std::string str() const

  31. {

  32. std::string tag = success? "SUCCESS": "FAILURE";

  33. return mooon::utils: :CStringUtils::format_string("[%s %s]: %u seconds", ip.c_str(), tag.c_str(), seconds);

  34. }

  35. };


  36. inline std::ostream& operator <<(std::ostream& out, const struct ResultInfo& result)

  37. {

  38. std::string tag = result.success? "SUCCESS": "FAILURE";

  39. out << "["PRINT_COLOR_YELLOW << result.ip << PRINT_COLOR_NONE" " << tag << "] " << result.seconds << " seconds";

  40. return out;

  41. }


  42. int main(int argc, char* argv[])

  43. {

  44. // Parse command line parameters

  45. std::string errmsg;

  46. if (!mooon::utils: :parse_arguments(argc, argv, &errmsg))

  47. {

  48. fprintf(stderr, "parameter error: %sn", errmsg.c_str());

  49. exit(1);

  50. }


  51. uint16_t port = mooon::argument::p ->value();

  52. std::string source = mooon::argument::s->value();

  53. std::string directory = mooon::argument::d->value();

  54. std::string hosts = mooon::argument::h->value();

  55. std::string user = mooon::argument::u->value();

  56. std::string password = mooon::argument::P->value( );

  57. mooon::utils::CStringUtils::trim(source);

  58. mooon::utils::CStringUtils::trim(directory);

  59. mooon::utils::CStringUtils::trim(hosts);

  60. mooon::utils::CStringUtils::trim(user);

  61. mooon::utils::CStringUtils::trim(password);


  62. // Check parameters (-s)

  63. if (source.empty())

  64. {

  65. fprintf(stderr, "parameter[-s]'s value not setn");

  66. exit(1);

  67. }


  68. // Check parameters (-d)

  69. if (directory.empty())

  70. {

  71. fprintf(stderr, "parameter[-d]'s value not setn") ;

  72. exit(1);

  73. }


  74. // Check parameters (-h )

  75. if (hosts.empty())

  76. {

  77. // Try to get the value from the environment variable

  78. const char* hosts_ = getenv("HOSTS");

  79. if (NULL == hosts_)

  80. {

  81. fprintf(stderr, "parameter[-h]'s value not setn");

  82. exit(1);

  83. }


  84. hosts= hosts_;

  85. mooon::utils::CStringUtils::trim(hosts);

  86. if (hosts .empty())

  87. {

  88. fprintf(stderr, "parameter[-h]'s value not setn");

  89. exit(1);

  90. }

  91. }


  92. // Check parameters (- u)

  93. if (user.empty())

  94. {
  95. fprintf(stderr, "parameter[-u]'s value not setn");
  96. exit(1);
  97. }

  98. // 检查参数(-P)
  99. if (password.empty())
  100. {
  101. fprintf(stderr, "parameter[-P]'s value not setn");
  102. exit(1);
  103. }

  104. std::vector hosts_ip;
  105. const std::string& remote_hosts_ip = hosts;
  106. int num_remote_hosts_ip = mooon::utils::CTokener::split(&hosts_ip, remote_hosts_ip, ",", true);
  107. if (0 == num_remote_hosts_ip)
  108. {
  109. fprintf(stderr, "parameter[-h] errorn");
  110. exit(1);
  111. }

  112. std::string remote_filepath = directory std::string("/") mooon::utils::CStringUtils::extract_filename(source);
  113. std::vector results(num_remote_hosts_ip);
  114. for (int i=0; i
  115. {
  116. bool color = true;
  117. const std::string& remote_host_ip = hosts_ip[i];
  118. results[i].ip = remote_host_ip;
  119. results[i].success = false;

  120. fprintf(stdout, "["PRINT_COLOR_YELLOW"%s"PRINT_COLOR_NONE"]n", remote_host_ip.c_str());
  121. fprintf(stdout, PRINT_COLOR_GREEN);

  122. mooon::sys::CStopWatch stop_watch;
  123. try
  124. {
  125. int file_size = 0;
  126. mooon::net::CLibssh2 libssh2(remote_host_ip, port, user, password, mooon::argument::t->value());
  127. libssh2.upload(source, remote_filepath, &file_size);

  128. fprintf(stdout, "["PRINT_COLOR_YELLOW"%s"PRINT_COLOR_NONE"] SUCCESS: %d bytesn", remote_host_ip.c_str(), file_size);
  129. results[i].success = true;
  130. }
  131. catch (mooon::sys::CSyscallException& ex)
  132. {
  133. if (color)
  134. fprintf(stdout, PRINT_COLOR_NONE); // color = true;

  135. fprintf(stderr, "["PRINT_COLOR_RED"%s"PRINT_COLOR_NONE"] failed: %sn", remote_host_ip.c_str(), ex.str().c_str());
  136. }
  137. catch (mooon::utils::CException& ex)
  138. {
  139. if (color)
  140. fprintf(stdout, PRINT_COLOR_NONE); // color = true;

  141. fprintf(stderr, "["PRINT_COLOR_RED"%s"PRINT_COLOR_NONE"] failed: %sn", remote_host_ip.c_str(), ex.str().c_str());
  142. }

  143. results[i].seconds = stop_watch.get_elapsed_microseconds() / 1000000;
  144. std::cout << std::endl;
  145. }

  146. // 输出总结
  147. std::cout << std::endl;
  148. std::cout << "================================" << std::endl;
  149. int num_success = 0; // 成功的个数
  150. int num_failure = 0; // 失败的个数
  151. for (std::vector::size_type i=0; i
  152. {
  153. const struct ResultInfo& result_info = results[i];
  154. std::cout << result_info << std::endl;

  155. if (result_info.success)
  156. num_success;
  157. else
  158. num_failure;
  159. }
  160. std::cout << "SUCCESS: " << num_success << ", FAILURE: " << num_failure << std::endl;

  161. return 0;
  162. }


www.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上传...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template