上傳單個檔案到多台機器工具_PHP教程

來源:互聯網
上載者:User

上傳單個檔案到多台機器工具


使用樣本:
./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/目錄

  1. #include "mooon/net/libssh2.h"
  2. #include "mooon/sys/stop_watch.h"
  3. #include "mooon/utils/args_parser.h"
  4. #include "mooon/utils/print_color.h"
  5. #include "mooon/utils/string_utils.h"
  6. #include "mooon/utils/tokener.h"
  7. #include
  8. #include

  9. // 逗號分隔的遠程主機列表
  10. STRING_ARG_DEFINE(h, "", "remote hosts");
  11. // 遠程主機的sshd連接埠號碼
  12. INTEGER_ARG_DEFINE(uint16_t, p, 22, 10, 65535, "remote hosts port");
  13. // 使用者名稱
  14. STRING_ARG_DEFINE(u, "root", "remote host user");
  15. // 密碼
  16. STRING_ARG_DEFINE(P, "", "remote host password");

  17. // 被上傳的檔案路徑
  18. STRING_ARG_DEFINE(s, "", "the source file uploaded");
  19. // 檔案上傳後存放的目錄路徑
  20. STRING_ARG_DEFINE(d, "", "the directory to store");

  21. // 連線逾時,單位為秒
  22. INTEGER_ARG_DEFINE(uint16_t, t, 10, 1, 65535, "timeout seconds to remote host");

  23. // 結果資訊
  24. struct ResultInfo
  25. {
  26. bool success; // 為true表示執行成功
  27. std::string ip; // 遠程host的IP地址
  28. uint32_t seconds; // 運行花費的時間長度,精確到秒

  29. ResultInfo()
  30. : success(false), seconds(0)
  31. {
  32. }

  33. std::string str() const
  34. {
  35. std::string tag = success? "SUCCESS": "FAILURE";
  36. return mooon::utils::CStringUtils::format_string("[%s %s]: %u seconds", ip.c_str(), tag.c_str(), seconds);
  37. }
  38. };

  39. inline std::ostream& operator <<(std::ostream& out, const struct ResultInfo& result)
  40. {
  41. std::string tag = result.success? "SUCCESS": "FAILURE";
  42. out << "["PRINT_COLOR_YELLOW << result.ip << PRINT_COLOR_NONE" " << tag << "] " << result.seconds << " seconds";
  43. return out;
  44. }

  45. int main(int argc, char* argv[])
  46. {
  47. // 解析命令列參數
  48. std::string errmsg;
  49. if (!mooon::utils::parse_arguments(argc, argv, &errmsg))
  50. {
  51. fprintf(stderr, "parameter error: %s\n", errmsg.c_str());
  52. exit(1);
  53. }

  54. uint16_t port = mooon::argument::p->value();
  55. std::string source = mooon::argument::s->value();
  56. std::string directory = mooon::argument::d->value();
  57. std::string hosts = mooon::argument::h->value();
  58. std::string user = mooon::argument::u->value();
  59. std::string password = mooon::argument::P->value();
  60. mooon::utils::CStringUtils::trim(source);
  61. mooon::utils::CStringUtils::trim(directory);
  62. mooon::utils::CStringUtils::trim(hosts);
  63. mooon::utils::CStringUtils::trim(user);
  64. mooon::utils::CStringUtils::trim(password);

  65. // 檢查參數(-s)
  66. if (source.empty())
  67. {
  68. fprintf(stderr, "parameter[-s]'s value not set\n");
  69. exit(1);
  70. }

  71. // 檢查參數(-d)
  72. if (directory.empty())
  73. {
  74. fprintf(stderr, "parameter[-d]'s value not set\n");
  75. exit(1);
  76. }

  77. // 檢查參數(-h)
  78. if (hosts.empty())
  79. {
  80. // 嘗試從環境變數取值
  81. const char* hosts_ = getenv("HOSTS");
  82. if (NULL == hosts_)
  83. {
  84. fprintf(stderr, "parameter[-h]'s value not set\n");
  85. exit(1);
  86. }

  87. hosts= hosts_;
  88. mooon::utils::CStringUtils::trim(hosts);
  89. if (hosts.empty())
  90. {
  91. fprintf(stderr, "parameter[-h]'s value not set\n");
  92. exit(1);
  93. }
  94. }

  95. // 檢查參數(-u)
  96. if (user.empty())
  97. {
  98. fprintf(stderr, "parameter[-u]'s value not set\n");
  99. exit(1);
  100. }

  101. // 檢查參數(-P)
  102. if (password.empty())
  103. {
  104. fprintf(stderr, "parameter[-P]'s value not set\n");
  105. exit(1);
  106. }

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

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

  123. fprintf(stdout, "["PRINT_COLOR_YELLOW"%s"PRINT_COLOR_NONE"]\n", remote_host_ip.c_str());
  124. fprintf(stdout, PRINT_COLOR_GREEN);

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

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

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

  144. fprintf(stderr, "["PRINT_COLOR_RED"%s"PRINT_COLOR_NONE"] failed: %s\n", remote_host_ip.c_str(), ex.str().c_str());
  145. }

  146. results[i].seconds = stop_watch.get_elapsed_microseconds() / 1000000;
  147. std::cout << std::endl;
  148. }

  149. // 輸出總結
  150. std::cout << std::endl;
  151. std::cout << "================================" << std::endl;
  152. int num_success = 0; // 成功的個數
  153. int num_failure = 0; // 失敗的個數
  154. for (std::vector::size_type i=0; i
  155. {
  156. const struct ResultInfo& result_info = results[i];
  157. std::cout << result_info << std::endl;

  158. if (result_info.success)
  159. ++num_success;
  160. else
  161. ++num_failure;
  162. }
  163. std::cout << "SUCCESS: " << num_success << ", FAILURE: " << num_failure << std::endl;

  164. return 0;
  165. }


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上傳...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.