>  기사  >  백엔드 개발  >  上传单个文件到多台机器工具_PHP教程

上传单个文件到多台机器工具_PHP教程

WBOY
WBOY원래의
2016-07-12 08:55:08897검색

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

使用示例:
./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
  40. {
  41. std::string tag = result.success? "SUCCESS": "FAILURE";
  42. out
  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<: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 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
  148. }

  149. // 输出总结
  150. std::cout
  151. std::cout
  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

  158. if (result_info.success)
  159. ++num_success;
  160. else
  161. ++num_failure;
  162. }
  163. std::cout

  164. return 0;
  165. }


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上传...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.