Table of Contents
上传单个文件到多台机器工具
Home php教程 php手册 上传单个文件到多台机器工具

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

Jun 13, 2016 am 08:42 AM
android

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

使用示例:
./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. }


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Motorola Razr 50s shows itself as possible new budget foldable in early leak Motorola Razr 50s shows itself as possible new budget foldable in early leak Sep 07, 2024 am 09:35 AM

Motorola has released countless devices this year, although only two of them are foldables. For context, while most of the world has received the pair as the Razr 50 and Razr 50 Ultra, Motorola offers them in North America as the Razr 2024 and Razr 2

See all articles