[关闭]
@tsing1226 2016-10-20T12:23:15.000000Z 字数 2148 阅读 1013

java

java远程调用shell脚本


对远程机器shell脚本赋值权限

  1. sudo chmod u+x /path/to/shellfilename.sh

新建项目,添加依赖

  1. <dependency>
  2. <groupId>org.jvnet.hudson</groupId>
  3. <artifactId>ganymed-ssh2</artifactId>
  4. <version>build210-hudson-1</version>
  5. </dependency>

编写工具类

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.nio.charset.Charset;
  4. import ch.ethz.ssh2.Connection;
  5. import ch.ethz.ssh2.Session;
  6. public class RemoteShellTool {
  7. private Connection conn;//连接
  8. private String ipAddr;//ip地址
  9. private String charset = Charset.defaultCharset().toString();
  10. private String userName;//用户名
  11. private String password;//密码
  12. /**
  13. * 构造函数 连接远程
  14. *
  15. * @param ipAddr
  16. * @param userName
  17. * @param password
  18. * @param charset
  19. */
  20. public RemoteShellTool(String ipAddr, String userName, String password,
  21. String charset) {
  22. this.ipAddr = ipAddr;
  23. this.userName = userName;
  24. this.password = password;
  25. if (charset != null) {
  26. this.charset = charset;
  27. }
  28. }
  29. public RemoteShellTool(String ipAddr, String userName, String password) {
  30. this.ipAddr = ipAddr;
  31. this.userName = userName;
  32. this.password = password;
  33. }
  34. /**
  35. * 登陆
  36. *
  37. * @return
  38. * @throws IOException
  39. */
  40. public boolean login() throws IOException {
  41. conn = new Connection(ipAddr);
  42. conn.connect(); // 连接
  43. if (conn.isAuthenticationComplete()) {
  44. System.out.println("是");
  45. }
  46. return conn.authenticateWithPassword(userName, password); // 认证
  47. }
  48. /**
  49. * 运行
  50. *
  51. * @param cmds
  52. * @return
  53. */
  54. public String exec(String cmds) {
  55. InputStream in = null;
  56. String result = "";
  57. try {
  58. if (this.login()) {
  59. Session session = conn.openSession(); // 打开一个会话
  60. session.execCommand(cmds);
  61. in = session.getStdout();
  62. result = this.processStdout(in, this.charset);
  63. session.close();
  64. conn.close();
  65. }
  66. } catch (IOException e1) {
  67. e1.printStackTrace();
  68. }
  69. return result;
  70. }
  71. /**
  72. * 标准输出
  73. *
  74. * @param in
  75. * @param charset
  76. * @return
  77. */
  78. public String processStdout(InputStream in, String charset) {
  79. byte[] buf = new byte[1024];
  80. StringBuffer sb = new StringBuffer();
  81. try {
  82. while (in.read(buf) != -1) {
  83. sb.append(new String(buf, charset));
  84. }
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. }
  88. return sb.toString();
  89. }
  90. /**
  91. * @param args
  92. * @throws IOException
  93. * @throws InterruptedException
  94. */
  95. public static void main(String[] args) throws IOException, InterruptedException {
  96. RemoteShellTool tool = new RemoteShellTool("10.7.111.117", "root","123456", "utf-8");
  97. String result = tool.exec("/home/root/test.sh");
  98. System.out.print(result);
  99. }
  100. }

运行测试

参考文献

http://blog.csdn.net/xiao_jun_0820/article/details/26254813

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注