java调用远程服务器shell脚本

绝地灬酷狼 2022-01-12 03:35 418阅读 0赞

1.代码

代码比较简单

  1. package com.springboot.information.utils;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.UnsupportedEncodingException;
  5. import java.nio.charset.Charset;
  6. import org.apache.commons.io.IOUtils;
  7. import ch.ethz.ssh2.ChannelCondition;
  8. import ch.ethz.ssh2.Connection;
  9. import ch.ethz.ssh2.Session;
  10. import ch.ethz.ssh2.StreamGobbler;
  11. public class RemoteShellExecutor {
  12. private Connection conn;
  13. /** 远程机器IP */
  14. private String ip;
  15. /** 用户名 */
  16. private String osUsername;
  17. /** 密码 */
  18. private String password;
  19. private String charset = Charset.defaultCharset().toString();
  20. private static final int TIME_OUT = 1000 * 5 * 60;
  21. /**
  22. * 构造函数
  23. * @param ip
  24. * @param usr
  25. * @param pasword
  26. */
  27. public RemoteShellExecutor(String ip, String usr, String pasword) {
  28. this.ip = ip;
  29. this.osUsername = usr;
  30. this.password = pasword;
  31. }
  32. /**
  33. * 登录
  34. * @return
  35. * @throws IOException
  36. */
  37. private boolean login() throws IOException {
  38. conn = new Connection(ip);
  39. conn.connect();
  40. return conn.authenticateWithPassword(osUsername, password);
  41. }
  42. /**
  43. * 执行脚本
  44. *
  45. * @param cmds
  46. * @return
  47. * @throws Exception
  48. */
  49. public int exec(String cmds) throws Exception {
  50. InputStream stdOut = null;
  51. InputStream stdErr = null;
  52. String outStr = "";
  53. String outErr = "";
  54. int ret = -1;
  55. try {
  56. if (login()) {
  57. System.out.println("login success");
  58. // Open a new {@link Session} on this connection
  59. Session session = conn.openSession();
  60. // Execute a command on the remote machine.
  61. System.out.println("session success");
  62. session.execCommand(cmds);
  63. System.out.println("exec:"+cmds);
  64. System.out.println("session:"+session);
  65. stdOut = new StreamGobbler(session.getStdout());
  66. System.out.println("stdOut:"+stdOut.toString());
  67. outStr = processStream(stdOut, charset);
  68. System.out.println("stdOut");
  69. stdErr = new StreamGobbler(session.getStderr());
  70. outErr = processStream(stdErr, charset);
  71. System.out.println("stdErr");
  72. session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
  73. System.out.println("outStr=" + outStr);
  74. System.out.println("outErr=" + outErr);
  75. ret = session.getExitStatus();
  76. } else {
  77. throw new Exception("登录远程机器失败" + ip); // 自定义异常类 实现略
  78. }
  79. } finally {
  80. if (conn != null) {
  81. conn.close();
  82. }
  83. IOUtils.closeQuietly(stdOut);
  84. IOUtils.closeQuietly(stdErr);
  85. }
  86. return ret;
  87. }
  88. /**
  89. * @param in
  90. * @param charset
  91. * @return
  92. * @throws IOException
  93. * @throws UnsupportedEncodingException
  94. */
  95. private String processStream(InputStream in, String charset) throws Exception {
  96. System.out.println("ininin");
  97. byte[] buf = new byte[1024];
  98. StringBuilder sb = new StringBuilder();
  99. int num =0;
  100. while (in.read(buf) != -1) {
  101. num++;
  102. System.out.println("number:"+num);
  103. sb.append(new String(buf, charset));
  104. System.out.println("sb:"+sb.toString());
  105. System.out.println("buf:"+in.read(buf));
  106. }
  107. System.out.println("enene22");
  108. return sb.toString();
  109. }
  110. public static void main(String args[]) throws Exception {
  111. RemoteShellExecutor executor = new RemoteShellExecutor("XXXX", "XX", "XXX");
  112. // 执行myTest.sh 参数为java Know dummy
  113. System.out.println(executor.exec("/home/zh/Documents/start/note.sh"));
  114. }
  115. }
  116. 复制代码

转载于:https://juejin.im/post/5d072f08518825092c7171d5

发表评论

表情:
评论列表 (有 0 条评论,418人围观)

还没有评论,来说两句吧...

相关阅读