Upload a single file to multiple machine tools _php Tutorial

Source: Internet
Author: User

Upload a single file to multiple machine tools


Examples of Use:
./mooon_upload-h=192.168.10.11,192.168.10.12-p=6000-u=root-p= ' root123 '-s=./abc-d=/tmp/
Represents a local file./abc uploaded to the/tmp/directory of two machines 192.168.10.11 and 192.168.10.12

  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. Comma separated list of remote hosts
  10. String_arg_define (H, "", "remote hosts");
  11. The sshd port number of the remote host
  12. Integer_arg_define (uint16_t, p, Max, 65535, "remote hosts port");
  13. User name
  14. String_arg_define (U, "root", "remote host user");
  15. Password
  16. String_arg_define (P, "", "Remote host password");

  17. File path to be uploaded
  18. String_arg_define (S, "", "the source file uploaded");
  19. directory path after file upload
  20. String_arg_define (D, "", "the Directory to store");

  21. Connection time-out, units in seconds
  22. Integer_arg_define (uint16_t, T, 1, 65535, "Timeout seconds to remote host");

  23. Result information
  24. struct RESULTINFO
  25. {
  26. BOOL success; True to indicate successful execution
  27. std::string IP; IP Address of remote host
  28. uint32_t seconds; Time spent on running, accurate to 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 << "]" << Res Ult.seconds << "seconds";
  43. return out;
  44. }

  45. int main (int argc, char* argv[])
  46. {
  47. Parsing command-line arguments
  48. std::string errmsg;
  49. if (!mooon::utils::p arse_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. Check parameter (-s)
  66. if (Source.empty ())
  67. {
  68. fprintf (stderr, "parameter[-s] 's value not set\n");
  69. Exit (1);
  70. }

  71. Check parameters (-D)
  72. if (Directory.empty ())
  73. {
  74. fprintf (stderr, "parameter[-d] 's value not set\n");
  75. Exit (1);
  76. }

  77. Check parameters (-h)
  78. if (Hosts.empty ())
  79. {
  80. Try to take a value from an environment variable
  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. Check parameters (-u)
  96. if (User.empty ())
  97. {
  98. fprintf (stderr, "parameter[-u] 's value not set\n");
  99. Exit (1);
  100. }

  101. Check parameters (-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. Output summary
  150. Std::cout << Std::endl;
  151. Std::cout << "================================" << Std::endl;
  152. int num_success = 0; Number of successes
  153. int num_failure = 0; Number of failures
  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.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117250.html techarticle upload a single file to multiple machine tools using example:./mooon_upload-h=192.168.10.11,192.168.10.12-p=6000-u=root-p= ' root123 '-s=./abc-d=/tmp/ Represents the file that will be local./ABC upload ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.