Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8049650
  • 博文数量: 594
  • 博客积分: 13065
  • 博客等级: 上将
  • 技术积分: 10324
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-26 16:44
个人简介

推荐: blog.csdn.net/aquester https://github.com/eyjian https://www.cnblogs.com/aquester http://blog.chinaunix.net/uid/20682147.html

文章分类

全部博文(594)

分类: LINUX

2016-04-01 09:48:17

使用示例(使用了默认用户root,和默认端口号22):
./mooon_ssh --h=192.168.4.1,192.168.4.2 -P=password -c='cat /etc/hosts'


  1. #include "mooon/net/libssh2.h" // 提供远程执行命令接口
  2. #include "mooon/sys/error.h"
  3. #include "mooon/sys/stop_watch.h"
  4. #include "mooon/utils/args_parser.h"
  5. #include "mooon/utils/print_color.h"
  6. #include "mooon/utils/string_utils.h"
  7. #include "mooon/utils/tokener.h"
  8. #include <iostream>

  9. // 被执行的命令,可为一条或多条命令,如:ls /&&whoami
  10. STRING_ARG_DEFINE(c, "", "command to execute remotely");
  11. // 逗号分隔的远程主机列表
  12. STRING_ARG_DEFINE(h, "", "remote hosts");
  13. // 远程主机的sshd端口号
  14. INTEGER_ARG_DEFINE(uint16_t, p, 22, 10, 65535, "remote hosts port");
  15. // 用户名
  16. STRING_ARG_DEFINE(u, "root", "remote host user");
  17. // 密码
  18. STRING_ARG_DEFINE(P, "", "remote host password");

  19. // 连接超时,单位为秒
  20. INTEGER_ARG_DEFINE(uint16_t, t, 10, 1, 65535, "timeout seconds to remote host");

  21. // 结果信息
  22. struct ResultInfo
  23. {
  24.     bool success; // 为true表示执行成功
  25.     std::string ip; // 远程host的IP地址
  26.     uint32_t seconds; // 运行花费的时长,精确到秒

  27.     ResultInfo()
  28.         : success(false), seconds(0)
  29.     {
  30.     }

  31.     std::string str() const
  32.     {
  33.         std::string tag = success? "SUCCESS": "FAILURE";
  34.         return mooon::utils::CStringUtils::format_string("[%s %s]: %u seconds", ip.c_str(), tag.c_str(), seconds);
  35.     }
  36. };

  37. inline std::ostream& operator <<(std::ostream& out, const struct ResultInfo& result)
  38. {
  39.     std::string tag = result.success? "SUCCESS": "FAILURE";
  40.     out << "["PRINT_COLOR_YELLOW << result.ip << PRINT_COLOR_NONE" " << tag << "] " << result.seconds << " seconds";
  41.     return out;
  42. }

  43. // 使用示例:
  44. // mooon_ssh -u=root -P=test -p=2016 -h="127.0.0.1,192.168.0.1" -c='ls /tmp&&ps aux|grep -c test'
  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 commands = mooon::argument::c->value();
  56.     std::string hosts = mooon::argument::h->value();
  57.     std::string user = mooon::argument::u->value();
  58.     std::string password = mooon::argument::P->value();
  59.     mooon::utils::CStringUtils::trim(commands);
  60.     mooon::utils::CStringUtils::trim(hosts);
  61.     mooon::utils::CStringUtils::trim(user);
  62.     mooon::utils::CStringUtils::trim(password);

  63.     // 检查参数(-c)
  64.     if (commands.empty())
  65.     {
  66.         fprintf(stderr, "parameter[-c]'s value not set\n");
  67.         exit(1);
  68.     }

  69.     // 检查参数(-h)
  70.     if (hosts.empty())
  71.     {
  72.         // 尝试从环境变量取值
  73.         const char* hosts_ = getenv("HOSTS");
  74.         if (NULL == hosts_)
  75.         {
  76.             fprintf(stderr, "parameter[-h]'s value not set\n");
  77.             exit(1);
  78.         }

  79.         hosts= hosts_;
  80.         mooon::utils::CStringUtils::trim(hosts);
  81.         if (hosts.empty())
  82.         {
  83.             fprintf(stderr, "parameter[-h]'s value not set\n");
  84.             exit(1);
  85.         }
  86.     }

  87.     // 检查参数(-u)
  88.     if (user.empty())
  89.     {
  90.         fprintf(stderr, "parameter[-u]'s value not set\n");
  91.         exit(1);
  92.     }

  93.     // 检查参数(-P)
  94.     if (password.empty())
  95.     {
  96.         fprintf(stderr, "parameter[-P]'s value not set\n");
  97.         exit(1);
  98.     }

  99.     std::vector<std::string> hosts_ip;
  100.     const std::string& remote_hosts_ip = hosts;
  101.     int num_remote_hosts_ip = mooon::utils::CTokener::split(&hosts_ip, remote_hosts_ip, ",", true);
  102.     if (0 == num_remote_hosts_ip)
  103.     {
  104.         fprintf(stderr, "parameter[-h] error\n");
  105.         exit(1);
  106.     }

  107.     std::vector<struct ResultInfo> results(num_remote_hosts_ip);
  108.     for (int i=0; i<num_remote_hosts_ip; ++i)
  109.     {
  110.         bool color = true;
  111.         int num_bytes = 0;
  112.         int exitcode = 0;
  113.         std::string exitsignal;
  114.         std::string errmsg;
  115.         const std::string& remote_host_ip = hosts_ip[i];
  116.         results[i].ip = remote_host_ip;

  117.         fprintf(stdout, "["PRINT_COLOR_YELLOW"%s"PRINT_COLOR_NONE"]\n", remote_host_ip.c_str());
  118.         fprintf(stdout, PRINT_COLOR_GREEN);

  119.         mooon::sys::CStopWatch stop_watch;
  120.         try
  121.         {
  122.             mooon::net::CLibssh2 libssh2(remote_host_ip, port, user, password, mooon::argument::t->value());

  123.             libssh2.remotely_execute(commands, std::cout, &exitcode, &exitsignal, &errmsg, &num_bytes);
  124.             fprintf(stdout, PRINT_COLOR_NONE);
  125.             color = false; // color = true;

  126.             if ((0 == exitcode) && exitsignal.empty())
  127.             {
  128.                 results[i].success = true;
  129.                 fprintf(stdout, "["PRINT_COLOR_YELLOW"%s"PRINT_COLOR_NONE"] SUCCESS\n", remote_host_ip.c_str());
  130.             }
  131.             else
  132.             {
  133.                 results[i].success = false;

  134.                 if (exitcode != 0)
  135.                 {
  136.                     fprintf(stderr, "%d: %s\n", exitcode, mooon::sys::Error::to_string(exitcode).c_str());
  137.                 }
  138.                 else if (!exitsignal.empty())
  139.                 {
  140.                     fprintf(stderr, "%s: %s\n", exitsignal.c_str(), errmsg.c_str());
  141.                 }
  142.             }
  143.         }
  144.         catch (mooon::sys::CSyscallException& ex)
  145.         {
  146.             if (color)
  147.                 fprintf(stdout, PRINT_COLOR_NONE); // color = true;

  148.             fprintf(stderr, "["PRINT_COLOR_RED"%s"PRINT_COLOR_NONE"] failed: %s\n", remote_host_ip.c_str(), ex.str().c_str());
  149.         }
  150.         catch (mooon::utils::CException& ex)
  151.         {
  152.             if (color)
  153.                 fprintf(stdout, PRINT_COLOR_NONE); // color = true;

  154.             fprintf(stderr, "["PRINT_COLOR_RED"%s"PRINT_COLOR_NONE"] failed: %s\n", remote_host_ip.c_str(), ex.str().c_str());
  155.         }

  156.         results[i].seconds = stop_watch.get_elapsed_microseconds() / 1000000;
  157.         std::cout << std::endl;
  158.     } // for

  159.     // 输出总结
  160.     std::cout << std::endl;
  161.     std::cout << "================================" << std::endl;
  162.     int num_success = 0; // 成功的个数
  163.     int num_failure = 0; // 失败的个数
  164.     for (std::vector<struct ResultInfo>::size_type i=0; i<results.size(); ++i)
  165.     {
  166.         const struct ResultInfo& result_info = results[i];
  167.         std::cout << result_info << std::endl;

  168.         if (result_info.success)
  169.             ++num_success;
  170.         else
  171.             ++num_failure;
  172.     }
  173.     std::cout << "SUCCESS: " << num_success << ", FAILURE: " << num_failure << std::endl;

  174.     return 0;
  175. }

阅读(4354) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~