Guest User

Untitled

a guest
Feb 15th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. String userName = "";
  2. String hostName = "";
  3. String password = "";
  4.  
  5. JSch javaSecureChannel = new JSch();
  6.  
  7. Session jschSession = null;
  8. Channel jschChannel = null;
  9.  
  10. try {
  11.  
  12. jschSession = javaSecureChannel.getSession(userName, hostName, 22);
  13. Properties configurationProperties = new Properties();
  14. configurationProperties.put("StrictHostKeyChecking", "no");
  15. jschSession.setConfig(configurationProperties);
  16. jschSession.setPassword(password);
  17. jschSession.connect();
  18. jschChannel = null;
  19. jschChannel = jschSession.openChannel("shell");
  20. jschChannel.setOutputStream(System.out);
  21.  
  22. File shellScript = createShellScript();
  23.  
  24. FileInputStream fin = new FileInputStream(shellScript);
  25. byte fileContent[] = new byte[(int) shellScript.length()];
  26. fin.read(fileContent);
  27. InputStream in = new ByteArrayInputStream(fileContent);
  28. jschChannel.setInputStream(in);
  29. jschChannel.connect();
  30.  
  31. while(true){
  32.  
  33. //if(jschChannel.isClosed)
  34. if(jschChannel.getExitStatus() == 0){
  35. System.out.println("exit-status: " + jschChannel.getExitStatus());
  36. break;
  37. }
  38.  
  39. try{
  40. Thread.sleep(1000);
  41. }catch(Exception ee){
  42. ee.printStackTrace();
  43. }
  44.  
  45. }
  46.  
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50.  
  51. jschChannel.disconnect();
  52. jschSession.disconnect();
  53. System.out.println("Done...!!!");
  54.  
  55. String temporaryShellFileName = "shellscript.sh";
  56. File fileStream = new File(temporaryShellFileName);
  57.  
  58. try {
  59.  
  60. PrintStream outStream = new PrintStream(new FileOutputStream(fileStream));
  61. outStream.println("#!/bin/bash");
  62. outStream.println("cd /u01/app/java/gids4x/Test");
  63. outStream.println("Test_with_NULL.sh");
  64. outStream.close();
  65.  
  66. } catch (Exception e) {
  67.  
  68. System.err.println("Error: " + e.getMessage());
  69.  
  70. }
  71.  
  72. public static void main(String[] arg){
  73.  
  74. Channel channel=null;
  75. Session session=null;
  76. try{
  77. JSch.setLogger(new MyLogger());
  78. JSch jsch=new JSch();
  79. jsch.addIdentity("C:\testLinuxKey.key");
  80.  
  81. String host=null;
  82. if(arg.length>0){
  83. host=arg[0];
  84. }
  85. else{
  86. host=JOptionPane.showInputDialog("Enter username@hostname",
  87. System.getProperty("user.name")+
  88. "@localhost");
  89. }
  90. String user=host.substring(0, host.indexOf('@'));
  91. host=host.substring(host.indexOf('@')+1);
  92.  
  93. session=jsch.getSession(user, host, 22);
  94.  
  95. // username and password will be given via UserInfo interface.
  96. UserInfo ui=new MyUserInfo();
  97. session.setUserInfo(ui);
  98.  
  99. session.connect();
  100.  
  101. channel=session.openChannel("shell");
  102.  
  103. //channel.setInputStream(System.in);
  104. channel.setOutputStream(System.out);
  105.  
  106. String temporaryShellFileName = "shellscript.sh";
  107. File fileStream = new File(temporaryShellFileName);
  108.  
  109. try {
  110.  
  111. PrintStream outStream = new PrintStream(new FileOutputStream(fileStream));
  112. outStream.println("id");
  113. outStream.println("sudo bash");
  114. outStream.println("cd /tmp/");
  115. outStream.println("/tmp/wasinfo.sh");
  116. outStream.println("exit");
  117. outStream.println("exit");
  118. outStream.close();
  119. FileInputStream fin = new FileInputStream(fileStream);
  120. byte fileContent[] = new byte[(int) fileStream.length()];
  121. fin.read(fileContent);
  122. InputStream in = new ByteArrayInputStream(fileContent);
  123. channel.setInputStream(in);
  124.  
  125. } catch (Exception e) {
  126.  
  127. System.err.println("Error: " + e.getMessage());
  128.  
  129. }
  130.  
  131. channel.connect();
  132. while(channel.isConnected())
  133. {
  134. System.out.println("----- Channel On ----");
  135. Thread.currentThread().sleep(5000);
  136. }
  137. channel.disconnect();
  138. session.disconnect();
  139. }
  140. catch(Exception e){
  141. System.out.println(e);
  142. channel.disconnect();
  143. session.disconnect();
  144. }
  145.  
  146. System.out.println("Main End");
  147. }
Add Comment
Please, Sign In to add comment