Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. package ms.mobile.oss.common;
  2.  
  3. import com.jcraft.jsch.ChannelExec;
  4. import com.jcraft.jsch.JSch;
  5. import com.jcraft.jsch.JSchException;
  6. import com.jcraft.jsch.Session;
  7. import org.apache.commons.io.IOUtils;
  8.  
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11.  
  12. public class SSHUtils {
  13.  
  14. private static final String ENCODING = "UTF-8";
  15.  
  16. public static Session getJSchSession(DestHost destHost) throws JSchException {
  17. JSch jsch = new JSch();
  18.  
  19. Session session = jsch.getSession(destHost.getUsername(), destHost.getHost(), destHost.getPort());
  20. session.setPassword(destHost.getPassword());
  21. session.setConfig("StrictHostKeyChecking", "no"); // 第一次访问服务器时不用输入yes
  22. session.setTimeout(destHost.getTimeout());
  23. session.connect();
  24.  
  25. return session;
  26. }
  27.  
  28. public static String execCommandByJSch(DestHost destHost, String command) throws IOException, JSchException {
  29. return execCommandByJSch(destHost, command, ENCODING);
  30. }
  31.  
  32. public static String execCommandByJSch(DestHost destHost, String command, String resultEncoding) throws IOException, JSchException {
  33. Session session = getJSchSession(destHost);
  34. String result = execCommandByJSch(session, command, resultEncoding);
  35. session.disconnect();
  36.  
  37. return result;
  38. }
  39.  
  40. public static String execCommandByJSch(Session session, String command) throws IOException, JSchException {
  41. return execCommandByJSch(session, command, ENCODING);
  42. }
  43.  
  44. public static String execCommandByJSch(Session session, String command, String resultEncoding) throws IOException, JSchException {
  45. ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
  46. InputStream in = channelExec.getInputStream();
  47. channelExec.setCommand(command);
  48. channelExec.setErrStream(System.err);
  49. channelExec.connect();
  50.  
  51. String result = IOUtils.toString(in, resultEncoding);
  52.  
  53. channelExec.disconnect();
  54.  
  55. return result;
  56. }
  57.  
  58. /**
  59. * 目标登录主机信息
  60. */
  61. public static class DestHost {
  62. private String host = "";
  63. private String username = "";
  64. private String password = "";
  65. private int port = 22;
  66. private int timeout = 60 * 60 * 1000;
  67.  
  68. public DestHost(String host, String username, String password) {
  69. this(host, username, password, 22, 60 * 60 * 1000);
  70. }
  71.  
  72. public DestHost(String host, String username, String password, int timeout) {
  73. this(host, username, password, 22, timeout);
  74. }
  75.  
  76. public DestHost(String host, String username, String password, int port, int timeout) {
  77. this.host = host;
  78. this.username = username;
  79. this.password = password;
  80. this.port = port;
  81. this.timeout = timeout;
  82. }
  83.  
  84. public String getHost() {
  85. return host;
  86. }
  87.  
  88. public void setHost(String host) {
  89. this.host = host;
  90. }
  91.  
  92. public String getUsername() {
  93. return username;
  94. }
  95.  
  96. public void setUsername(String username) {
  97. this.username = username;
  98. }
  99.  
  100. public String getPassword() {
  101. return password;
  102. }
  103.  
  104. public void setPassword(String password) {
  105. this.password = password;
  106. }
  107.  
  108. public int getPort() {
  109. return port;
  110. }
  111.  
  112. public void setPort(int port) {
  113. this.port = port;
  114. }
  115.  
  116. public int getTimeout() {
  117. return timeout;
  118. }
  119.  
  120. public void setTimeout(int timeout) {
  121. this.timeout = timeout;
  122. }
  123.  
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement