Guest User

Untitled

a guest
Jul 24th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. sshCommManager.sendCommand("cmd /c java -Xms256M -Xmx1024M -jar FileCatalystTester.jar -basic /Y");
  2.  
  3. public class SSHCommManager extends Observable{
  4.  
  5. private JSch jsch; //ssh library
  6. private static String user = "what";
  7. private static String password = "youwhat";
  8. //private static String host = "192.168.1.1";
  9. private static SSHCommManager sshCommManager;
  10. private Session session;
  11. private Channel channel;
  12. public boolean commsConnected = false;
  13. private int aPort = 22;
  14.  
  15. private SSHCommManager() {
  16.  
  17. this.addObserver(MainUI.getInstance());
  18. }
  19.  
  20. public static SSHCommManager getInstance() {
  21. if (sshCommManager == null) {
  22. sshCommManager = new SSHCommManager();
  23. }
  24. return sshCommManager;
  25. }
  26.  
  27. public void init(String aHost) {
  28.  
  29. try {
  30. jsch = new JSch();
  31.  
  32. //System.out.println("Getting ssh session...");
  33. session = jsch.getSession(user, aHost, aPort);
  34. session.setX11Host(aHost);
  35. session.setX11Port(aPort + 6000);
  36.  
  37. //System.out.println("Getting user info...");
  38. session.setPassword(password);
  39.  
  40. java.util.Properties config = new java.util.Properties();
  41. config.put("StrictHostKeyChecking", "no");
  42. session.setConfig(config);
  43.  
  44. System.out.println("Connecting to ssh...");
  45. session.connect(30000);
  46.  
  47. if (session.isConnected()) {
  48. commsConnected = true;
  49. } else {
  50. commsConnected = false;
  51. }
  52.  
  53. System.out.println(commsConnected);
  54.  
  55. } catch (Exception e) {
  56. System.out.println(e);
  57. }
  58.  
  59. }
  60.  
  61.  
  62. public void sendCommandTest(String aCommand) {
  63. try {
  64. Channel channel1=session.openChannel("shell");//only shell
  65. System.out.println("Sending Test command: "+ aCommand);
  66. channel1.setOutputStream(System.out);
  67. PrintStream shellStream = new PrintStream(channel1.getOutputStream()); // printStream for convenience
  68. channel1.connect();
  69. shellStream.println(aCommand);
  70. } catch (Exception e) {
  71. System.out.println(e);
  72. }
  73.  
  74. if (channel.isClosed()) {
  75. System.out.println("exit-status: "
  76. + channel.getExitStatus());
  77. }
  78. }
  79.  
  80. public String sendCommand(String aCommand){
  81.  
  82. InputStream in = null;
  83. OutputStream out = null;
  84. StringBuilder commandOut = new StringBuilder();
  85.  
  86. try {
  87.  
  88. channel = session.openChannel("exec");
  89. System.out.println("Sending command: " + aCommand);
  90. ((ChannelExec) channel).setCommand(aCommand);
  91. //channel.setInputStream(System.in);
  92. channel.setInputStream(null);
  93. //channel.setOutputStream(System.out);
  94. ((ChannelExec) channel).setErrStream(System.err);
  95.  
  96. in = channel.getInputStream();
  97. //out = channel.getOutputStream();
  98.  
  99. channel.connect();
  100.  
  101. byte[] tmp = new byte[1024];
  102. while (true) {
  103. while (in.available() > 0) {
  104. int i = in.read(tmp, 0, 1024);
  105. if (i < 0)break;
  106. //System.out.print(new String(tmp, 0, i));
  107. //System.out.println(channel.getInputStream().toString());
  108. commandOut.append(new String(tmp, 0, i));
  109.  
  110. //setChanged();
  111. //notifyObservers(System.err.toString() + "n");
  112. }
  113. if (channel.isClosed()) {
  114. System.out.println("exit-status: "
  115. + channel.getExitStatus());
  116. break;
  117. }
  118. try {
  119. Thread.sleep(1000);
  120. } catch (Exception ee) {
  121. throw new JSchException("Cannot execute remote command: " + aCommand + " : " + ee.getMessage());
  122. }
  123. }
  124.  
  125. //channel.disconnect();
  126. //session.disconnect();
  127.  
  128. } catch (Exception e) {
  129. System.out.println(e);
  130. }
  131.  
  132. return commandOut.toString();
  133.  
  134. }
  135.  
  136. public void cleanupSSH() {
  137.  
  138. channel.disconnect();
  139. session.disconnect();
  140. }
  141.  
  142. public boolean isCommsConnected() {
  143. return commsConnected;
  144. }
  145. }
Add Comment
Please, Sign In to add comment