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
- #include "Mooon/net/libssh2.h"
- #include "mooon/sys/stop_watch.h"
- #include "Mooon/utils/args_parser.h"
- #include "Mooon/utils/print_color.h"
- #include "Mooon/utils/string_utils.h"
- #include "Mooon/utils/tokener.h"
- #include
- #include
- Comma separated list of remote hosts
- String_arg_define (H, "", "remote hosts");
- The sshd port number of the remote host
- Integer_arg_define (uint16_t, p, Max, 65535, "remote hosts port");
- User name
- String_arg_define (U, "root", "remote host user");
- Password
- String_arg_define (P, "", "Remote host password");
- File path to be uploaded
- String_arg_define (S, "", "the source file uploaded");
- directory path after file upload
- String_arg_define (D, "", "the Directory to store");
- Connection time-out, units in seconds
- Integer_arg_define (uint16_t, T, 1, 65535, "Timeout seconds to remote host");
- Result information
- struct RESULTINFO
- {
- BOOL success; True to indicate successful execution
- std::string IP; IP Address of remote host
- uint32_t seconds; Time spent on running, accurate to seconds
- Resultinfo ()
- : Success (false), seconds (0)
- {
- }
- std::string str () const
- {
- std::string tag = success? "SUCCESS": "FAILURE";
- return mooon::utils::cstringutils::format_string ("[%s%s]:%u seconds", ip.c_str (), Tag.c_str (), seconds);
- }
- };
- Inline std::ostream& operator << (std::ostream& out, const struct resultinfo& result)
- {
- std::string tag = result.success? "SUCCESS": "FAILURE";
- Out << "[" Print_color_yellow << result.ip << print_color_none "" << tag << "]" << Res Ult.seconds << "seconds";
- return out;
- }
- int main (int argc, char* argv[])
- {
- Parsing command-line arguments
- std::string errmsg;
- if (!mooon::utils::p arse_arguments (argc, argv, &errmsg))
- {
- fprintf (stderr, "parameter error:%s\n", Errmsg.c_str ());
- Exit (1);
- }
- uint16_t port = mooon::argument::p->value ();
- std::string Source = Mooon::argument::s->value ();
- std::string directory = mooon::argument::d->value ();
- std::string hosts = Mooon::argument::h->value ();
- std::string user = Mooon::argument::u->value ();
- std::string password = mooon::argument::P->value ();
- Mooon::utils::cstringutils::trim (source);
- Mooon::utils::cstringutils::trim (directory);
- Mooon::utils::cstringutils::trim (hosts);
- Mooon::utils::cstringutils::trim (user);
- Mooon::utils::cstringutils::trim (password);
- Check parameter (-s)
- if (Source.empty ())
- {
- fprintf (stderr, "parameter[-s] 's value not set\n");
- Exit (1);
- }
- Check parameters (-D)
- if (Directory.empty ())
- {
- fprintf (stderr, "parameter[-d] 's value not set\n");
- Exit (1);
- }
- Check parameters (-h)
- if (Hosts.empty ())
- {
- Try to take a value from an environment variable
- Const char* HOSTS_ = getenv ("hosts");
- if (NULL = = Hosts_)
- {
- fprintf (stderr, "parameter[-h] 's value not set\n");
- Exit (1);
- }
- Hosts= Hosts_;
- Mooon::utils::cstringutils::trim (hosts);
- if (Hosts.empty ())
- {
- fprintf (stderr, "parameter[-h] 's value not set\n");
- Exit (1);
- }
- }
- Check parameters (-u)
- if (User.empty ())
- {
- fprintf (stderr, "parameter[-u] 's value not set\n");
- Exit (1);
- }
- Check parameters (-p)
- if (Password.empty ())
- {
- fprintf (stderr, "parameter[-p] 's value not set\n");
- Exit (1);
- }
- Std::vector hosts_ip;
- Const std::string& REMOTE_HOSTS_IP = hosts;
- int num_remote_hosts_ip = Mooon::utils::ctokener::split (&hosts_ip, Remote_hosts_ip, ",", true);
- if (0 = = num_remote_hosts_ip)
- {
- fprintf (stderr, "parameter[-h] error\n");
- Exit (1);
- }
- std::string remote_filepath = Directory + std::string ("/") + mooon::utils::cstringutils::extract_filename (source);
- Std::vector Results (NUM_REMOTE_HOSTS_IP);
- for (int i=0; i
- {
- BOOL color = true;
- Const std::string& REMOTE_HOST_IP = Hosts_ip[i];
- Results[i].ip = remote_host_ip;
- Results[i].success = false;
- fprintf (stdout, "[" Print_color_yellow "%s" Print_color_none "]\n", Remote_host_ip.c_str ());
- fprintf (stdout, print_color_green);
- Mooon::sys::cstopwatch Stop_watch;
- Try
- {
- int file_size = 0;
- Mooon::net::clibssh2 Libssh2 (remote_host_ip, port, user, password, mooon::argument::t->value ());
- Libssh2.upload (source, Remote_filepath, &file_size);
- fprintf (stdout, "[" Print_color_yellow "%s" Print_color_none "] SUCCESS:%d bytes\n", Remote_host_ip.c_str (), file_size) ;
- Results[i].success = true;
- }
- catch (mooon::sys::csyscallexception& ex)
- {
- if (color)
- fprintf (stdout, print_color_none); color = true;
- fprintf (stderr, "[" Print_color_red "%s" Print_color_none "] failed:%s\n", Remote_host_ip.c_str (), Ex.str (). C_STR ());
- }
- catch (mooon::utils::cexception& ex)
- {
- if (color)
- fprintf (stdout, print_color_none); color = true;
- fprintf (stderr, "[" Print_color_red "%s" Print_color_none "] failed:%s\n", Remote_host_ip.c_str (), Ex.str (). C_STR ());
- }
- Results[i].seconds = Stop_watch.get_elapsed_microseconds ()/1000000;
- Std::cout << Std::endl;
- }
- Output summary
- Std::cout << Std::endl;
- Std::cout << "================================" << Std::endl;
- int num_success = 0; Number of successes
- int num_failure = 0; Number of failures
- For (std::vector :: Size_type i=0; i
- {
- const struct resultinfo& result_info = results[i];
- Std::cout << result_info << Std::endl;
- if (result_info.success)
- ++num_success;
- Else
- ++num_failure;
- }
- Std::cout << "SUCCESS:" << num_success << ", FAILURE:" << num_failure << Std::endl;
- return 0;
- }
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 ...