当前位置:   article > 正文

xshell 获取远程linux服务器 cpu、磁盘、内存_用xshell连街linux怎么看磁盘空间

用xshell连街linux怎么看磁盘空间
  1. package com.sinosoft.common.utils;
  2. import com.jcraft.jsch.*;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.text.DecimalFormat;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. /**
  13. * @Author zyq
  14. * @Date 2020/5/29
  15. * @Description: 获取远程linux服务器 cpu、磁盘、内存
  16. */
  17. public class LinuxStateForShell {
  18. public static final String CPU_MEM_SHELL = "top -b -n 1";
  19. public static final String FILES_SHELL = "df -hl";
  20. public static final String[] COMMANDS = {CPU_MEM_SHELL, FILES_SHELL};
  21. public static final String LINE_SEPARATOR = System.getProperty("line.separator");
  22. private static Session session;
  23. /**
  24. * 连接到指定的HOST
  25. *
  26. * @return isConnect
  27. * @throws JSchException JSchException
  28. */
  29. private static boolean connect(String user, String passwd, String host) {
  30. JSch jsch = new JSch();
  31. try {
  32. session = jsch.getSession(user, host, 22);
  33. session.setPassword(passwd);
  34. java.util.Properties config = new java.util.Properties();
  35. config.put("StrictHostKeyChecking", "no");
  36. session.setConfig(config);
  37. session.connect();
  38. } catch (JSchException e) {
  39. e.printStackTrace();
  40. System.out.println("connect error !");
  41. return false;
  42. }
  43. return true;
  44. }
  45. /**
  46. * 远程连接Linux 服务器 执行相关的命令
  47. *
  48. * @param commands 执行的脚本
  49. * @param user 远程连接的用户名
  50. * @param passwd 远程连接的密码
  51. * @param host 远程连接的主机IP
  52. * @return 最终命令返回信息
  53. */
  54. public static Map runDistanceShell(String[] commands, String user, String passwd, String host) {
  55. if (!connect(user, passwd, host)) {
  56. return null;
  57. }
  58. Map map = new HashMap<>();
  59. StringBuilder stringBuffer;
  60. BufferedReader reader = null;
  61. Channel channel = null;
  62. try {
  63. for (String command : commands) {
  64. stringBuffer = new StringBuilder();
  65. channel = session.openChannel("exec");
  66. ((ChannelExec) channel).setCommand(command);
  67. channel.setInputStream(null);
  68. ((ChannelExec) channel).setErrStream(System.err);
  69. channel.connect();
  70. InputStream in = channel.getInputStream();
  71. reader = new BufferedReader(new InputStreamReader(in));
  72. String buf;
  73. while ((buf = reader.readLine()) != null) {
  74. //舍弃PID 进程信息
  75. if (buf.contains("PID")) {
  76. break;
  77. }
  78. stringBuffer.append(buf.trim()).append(LINE_SEPARATOR);
  79. }
  80. //每个命令存储自己返回数据-用于后续对返回数据进行处理
  81. map.put(command, stringBuffer.toString());
  82. }
  83. } catch (IOException | JSchException e) {
  84. e.printStackTrace();
  85. } finally
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/154114
推荐阅读
相关标签
  

闽ICP备14008679号