程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 上傳單個文件到多台機器工具

上傳單個文件到多台機器工具

編輯:關於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/目錄

  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 <fstream>
  8. #include <iostream>

  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<std::string> 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<struct ResultInfo> results(num_remote_hosts_ip);
  117. for (int i=0; i<num_remote_hosts_ip; ++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<struct ResultInfo>::size_type i=0; i<results.size(); ++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. }


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved