1、项目中需要检测到几台远程服务器的参数,差了很多资料,决定用的这个

2、jar包:ganymed-ssh2-build210.jar

3、原理:向远程linux服务器发送脚本命令,得到该台服务器的信息

4、代码如下:

public class Basic {public static void main(String[] args) {String hostname1 = "";String username1 = "";String password1 = "";String hostname2 = "";String username2 = "";String password2 = "";String hostname3 = "";String username3 = "";String password3 = "";/*** 服务器1*/Montor montor1 = getMontor(hostname1, username1, password1);/*** 服务器2*/Montor montor2 = getMontor(hostname2, username2, password2);/*** 服务器3*/Montor montor3 = getMontor(hostname3, username3, password3);System.out.println(montor1.toString());System.out.println(montor2.toString());System.out.println(montor3.toString());}private static Montor getMontor(String hostname, String username,String password) {Montor montor = null;try {Connection conn = new Connection(hostname);conn.connect();boolean isAuthenticated;isAuthenticated = conn.authenticateWithPassword(username, password);if (isAuthenticated == false)throw new IOException("Authentication failed.");montor = new Montor();montor.setHostName(exec(conn, "hostname"));montor.setDiskSpace(exec(conn, "df -h | awk 'NR==2 {print $2}'"));montor.setUserSpace(exec(conn, "df -h | awk 'NR==2 {print $3}'"));montor.setRemainingSpace(exec(conn,"df -h | awk 'NR==2 {print $4}'"));montor.setMemory(exec(conn, "free -m | awk 'NR==2 {print $2}'"));montor.setUseMemory(exec(conn, "free -m | awk 'NR==2 {print $3}'"));montor.setRemainingMemory(exec(conn,"free -m | awk 'NR==2 {print $4}'"));BigDecimal b1 = new BigDecimal(exec(conn,"free -m | awk 'NR==2 {print $3}'"));BigDecimal b2 = new BigDecimal(exec(conn,"free -m | awk 'NR==2 {print $2}'"));montor.setUsageMemory((b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP)).doubleValue()* 100 + "%");conn.close();} catch (IOException e) {// TODO Auto-generated catch block
            e.printStackTrace();}return montor;};/*** * @param conn 连接* @param command 执行的sheel命令* @return*/private static String exec(Connection conn, String command) {String data = "";try {Session sess = conn.openSession();//执行命令
            sess.execCommand(command);InputStream stdout = new StreamGobbler(sess.getStdout());BufferedReader br = new BufferedReader(new InputStreamReader(stdout));while (true) {String line = br.readLine();if (line == null)break;data = line;}br.close();sess.close();} catch (IOException e) {e.printStackTrace(System.err);System.exit(2);}return data;}
}

5、目前还需要服务器中各项服务的联通性,如tomcat,active-mq等服务 是否挂机,如有大婶知道,望告知

6、如果还有其他更好的方式望周知。

转载于:https://www.cnblogs.com/volare/p/4318824.html

使用ganymed-ssh2-build通过ssh获得远程服务器参数相关推荐

  1. iterm2 ssh 乱码_【已解决】Mac中iTerm2通过SSH连接远程服务器

    折腾: [未解决]Mac中用SecureCRT无法用rz上传文件到CentOS服务器 期间,又看到一个: 看到前面不止一个人说是: 给iTerm2安装zmodem ->难道是iTerm2,可以像 ...

  2. arm服务器获取文件路径中文,ssh 访问远程服务器文件路径

    ssh 访问远程服务器文件路径 内容精选 换一换 在IntelliJ上选择"项目",找到".idea"文件夹,单击右键选择"新建>文件" ...

  3. python写一个ssh工具_用Python写个自动ssh登录远程服务器的小工具

    很多时候我们喜欢在自己电脑的终端直接ssh连接Linux服务器,而不喜欢使用那些有UI界面的工具区连接我们的服务器.可是在终端使用ssh我们每次都需要输入账号和密码,这也是一个烦恼,所以我们可以简单的 ...

  4. aws linux使用ssh登陆_【Linux】 使用ssh连接远程服务器

    [Linux] 使用ssh连接远程服务器 前言:SSH,英文全称是Secure Shell Protocol(安全的壳程序协议).SSH是现在公司基本上都在使用的一种文字接口的加密传输技术,采用的是非 ...

  5. Mac下ssh连接远程服务器时自动断开问题

    在mac下使用securecrt通过ssh连接远程服务器时,总会一段时间没有动作后,ssh被自动断开.在windows下用xmanager貌似没有遇到过这个问题. 在网上找了解决方法如下: 客户端配置 ...

  6. 简单介绍使用 ssh 连接远程服务器运行图形界面程序

    使用 SSH 远程连接运行图形界面程序  windwiny.20080310  简单介绍使用 ssh 连接远程服务器运行图形界面程序.  一.服务器安装运行SSH服务.在Ubuntu下:  安装 代码 ...

  7. window系统使用ssh连接远程服务器

    window系统使用ssh连接远程服务器 准备 本地创建并配置ssh密钥 第一步:创建ssh密钥 第二步:创建config文件并配置相关信息 配置远程服务器 查看sshd服务的端口 新增ssh端口 删 ...

  8. ssh查看服务器上的文件,ssh 访问远程服务器文件路径

    ssh 访问远程服务器文件路径 内容精选 换一换 在IntelliJ上选择"项目",找到".idea"文件夹,单击右键选择"新建>文件" ...

  9. 【Linux】ssh连接远程服务器

    [Linux]ssh连接远程服务器 1.背景 2.实例 1.背景 Secure Shell(SSH)是一种加密网络协议,用于在不安全的网络上安全地运行网络服务. 利用SSH可以实现加密并安全地远程登录 ...

最新文章

  1. 城市大脑全球标准研究2:如何理解和定义城市大脑?
  2. leetcode 43. 字符串相乘(Multiply Strings)
  3. React Native 加载多类型布局的实现——分类列表SectionList的封装
  4. OCP Java 自测
  5. 论文小综 | Using External Knowledge on VQA
  6. 编译原理——实验叁——基于YACC的TINY语法分析器的构建
  7. 过气旗舰不如?刘作虎确认一加新机:比一加7 Pro更超值
  8. red hat linux综合实验报告,实验一 Red Hat Linux 9.doc
  9. 无向图是欧拉图的充要条件_500页开放书搞定概率图建模,图灵奖得主Judea Pearl推荐...
  10. OFFICE技术讲座:关于坐标、位置的术语说明
  11. FFMPEG实现RTSP中H264数据流解码 并且实时播放
  12. Android TimePicker
  13. elementui实现横向时间轴_element ui step组件在另一侧加时间轴显示
  14. 【Python】三维画图——matplotlib以及np.mgrid的使用
  15. raphael用法学习
  16. Salesforce系列(五):Salesforce Apex基础SOSL查询和数据添加!
  17. 复杂背景下的自动驾驶目标检测数据集
  18. #爬取豆瓣电影top250
  19. 【数据库】彻底理解外键的作用
  20. 全民一起玩Python基础篇第六课:复杂结构初步(列表、对象、模块等)(下)

热门文章

  1. POJ-2251 Dungeon Master
  2. 容器生态系统 - 每天5分钟玩转容器技术(2)
  3. 代理 block 通知传值
  4. 未授权用户在此计算机上的的请求登陆类型
  5. 浅谈代码的执行效率(4):汇编优化
  6. python数据可视化库 动态的_python --数据可视化(一)
  7. 5G 行业专网 — 三大运营商的 5G 专网类型
  8. 边缘计算 — 与 AI
  9. Openstack 通过 SQLAlchemy-ORM 访问数据库
  10. NR 5G 非3GPP 接入到核心网络的安全性