Guest User

Untitled

a guest
Sep 19th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. package utils;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7.  
  8. import net.sf.expectit.Expect;
  9. import net.sf.expectit.ExpectBuilder;
  10. import net.sf.expectit.matcher.Matchers;
  11.  
  12. import com.jcraft.jsch.Channel;
  13. import com.jcraft.jsch.ChannelSftp;
  14. import com.jcraft.jsch.JSch;
  15. import com.jcraft.jsch.JSchException;
  16. import com.jcraft.jsch.Session;
  17. import com.jcraft.jsch.SftpException;
  18.  
  19. /**
  20. * These 2 dependencies are required to run/compile this class:
  21. * <dependency>
  22. * <groupId>com.jcraft</groupId>
  23. * <artifactId>jsch</artifactId>
  24. * <version>0.1.51</version>
  25. * </dependency>
  26. * <dependency>
  27. * <groupId>net.sf.expectit</groupId>
  28. * <artifactId>expectit-core</artifactId>
  29. * <version>0.5.0</version>
  30. * </dependency>
  31. **/
  32. public class SSH {
  33.  
  34. public enum ChannelType {
  35.  
  36. SHELL, SFTP;
  37.  
  38. public boolean isSftp() {
  39. return this.equals(SFTP);
  40. }
  41. };
  42.  
  43. private static final String HOST = "HOST";
  44. private static final String USERNAME = "USERNAME";
  45. private static final String PASSWORD = "PASSWD";
  46. public static final String FILENAME = "FILE";
  47.  
  48. private Session session;
  49. private Channel channel;
  50. private ChannelType channelType = ChannelType.SHELL;
  51. private Expect expect;
  52.  
  53. public SSH() throws JSchException, IOException {
  54. initialize();
  55. }
  56.  
  57. public SSH(ChannelType channelType) throws JSchException, IOException {
  58. this.channelType = channelType;
  59. initialize();
  60. }
  61.  
  62. public String runCmd(String cmd, boolean... fetchResult) throws JSchException, IOException {
  63. if (channelType.isSftp()) {
  64. throw new UnsupportedOperationException("For the RUN operation, ChannelType must be SHELL.");
  65. }
  66.  
  67. reconnectSSHIfNecessary();
  68. expect.sendLine(cmd).expect(Matchers.contains("$"));
  69. return fetchResult.length > 0 && fetchResult[0] ? printResult() : "";
  70. }
  71.  
  72. public boolean put(byte[] file) throws FileNotFoundException, SftpException {
  73. if (!channelType.isSftp()) {
  74. throw new UnsupportedOperationException("For the PUT operation, ChannelType must be SFTP.");
  75. }
  76.  
  77. ByteArrayInputStream bis = new ByteArrayInputStream(file);
  78. ((ChannelSftp) channel).put(bis, FILENAME + ".tar.gz");
  79.  
  80. return true;
  81. }
  82.  
  83. public void disconnect() throws JSchException, IOException {
  84. if (channelType.isSftp()) {
  85. ((ChannelSftp) channel).exit();
  86. } else {
  87. expect.sendLine("exit");
  88. expect.close();
  89. }
  90.  
  91. channel.disconnect();
  92. session.disconnect();
  93. }
  94.  
  95. private void initialize() throws JSchException, IOException {
  96. JSch jSch = new JSch();
  97. session = jSch.getSession(USERNAME, HOST);
  98. session.setPassword(PASSWORD);
  99. session.setConfig("StrictHostKeyChecking", "no");
  100. session.connect();
  101.  
  102. channel = session.openChannel(channelType.toString().toLowerCase());
  103. expect = new ExpectBuilder()
  104. .withOutput(channel.getOutputStream())
  105. .withInputs(channel.getInputStream(), channel.getExtInputStream())
  106. .build();
  107.  
  108. channel.connect();
  109.  
  110. if (!channelType.isSftp()) {
  111. runCmd("stty -echo");
  112. }
  113. }
  114.  
  115. private void reconnectSSHIfNecessary() throws JSchException, IOException {
  116. if (!session.isConnected() || channel.isClosed()) {
  117. initialize();
  118. }
  119. }
  120.  
  121. private String printResult() throws IOException {
  122. expect.sendLine("exit");
  123. InputStream in = channel.getInputStream();
  124. int c;
  125. String result = "";
  126. while ((c = in.read()) != -1) {
  127. result += (char) c;
  128. }
  129.  
  130. return result.substring(0, result.length());
  131. }
  132.  
  133. }
Add Comment
Please, Sign In to add comment