Guest User

Untitled

a guest
Jul 12th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. public static void pipeStream(InputStream input, OutputStream output)
  2. throws IOException
  3. {
  4. byte buffer[] = new byte[1024];
  5. int numRead = 0;
  6.  
  7. do
  8. {
  9. numRead = input.read(buffer);
  10. output.write(buffer, 0, numRead);
  11. } while (input.available() > 0);
  12.  
  13. output.flush();
  14. }
  15.  
  16. public static void main(String[] argv)
  17. {
  18. FileInputStream fileIn = null;
  19. FileOutputStream fileOut = null;
  20.  
  21. OutputStream procIn = null;
  22. InputStream procOut = null;
  23.  
  24. try
  25. {
  26. fileIn = new FileInputStream("test.txt");
  27. fileOut = new FileOutputStream("testOut.txt");
  28.  
  29. Process process = Runtime.getRuntime().exec ("/bin/cat");
  30. procIn = process.getOutputStream();
  31. procOut = process.getInputStream();
  32.  
  33. pipeStream(fileIn, procIn);
  34. pipeStream(procOut, fileOut);
  35. }
  36. catch (IOException ioe)
  37. {
  38. System.out.println(ioe);
  39. }
  40. }
  41.  
  42. import java.io.FileInputStream;
  43. import java.io.IOException;
  44. import java.io.InputStream;
  45. import java.util.Properties;
  46.  
  47. import com.jcraft.jsch.Channel;
  48. import com.jcraft.jsch.ChannelExec;
  49. import com.jcraft.jsch.JSch;
  50. import com.jcraft.jsch.Session;
  51.  
  52. public class Connection
  53. {
  54. public static void setConnection(String hostName)
  55. {
  56. String host = hostName;
  57. Properties prop = new Properties();
  58. InputStream input = null;
  59.  
  60. try
  61. {
  62.  
  63. input = new FileInputStream("./config/finalvalidation.properties");
  64. Log.log("Validation of the Host Started: " + host);
  65.  
  66. prop.load(input);
  67. String user = prop.getProperty("username");
  68. Log.log("Username : " + user);
  69.  
  70. String password = prop.getProperty("password");
  71. Log.log("Password in property file is: " + password);
  72.  
  73. String privateKey = prop.getProperty("ppkfile");
  74.  
  75. Log.log("Password: " + password);
  76. Log.log("ppkfile: " + privateKey);
  77.  
  78. String command1 = prop.getProperty("command");
  79. Log.log("command: " + command1);
  80.  
  81. java.util.Properties config = new java.util.Properties();
  82. config.put("StrictHostKeyChecking", "no");
  83. JSch jsch = new JSch();
  84. Session session = jsch.getSession(user, host, 22);
  85.  
  86. if (password != null && !password.isEmpty() && (privateKey == null ||
  87. privateKey.isEmpty()))
  88. {
  89. session.setPassword(password);
  90. Log.log("Password identity added ");
  91. }
  92. else if (privateKey != null && !privateKey.isEmpty() && (password == null ||
  93. password.isEmpty()))
  94. {
  95. jsch.addIdentity(privateKey);
  96. Log.log("PPK identity added ");
  97. }
  98. else
  99. {
  100. Log.log("Please correct Password or PPK file placement in
  101. finalvalidation.properties");
  102. }
  103.  
  104. session.setConfig(config);
  105. session.setPassword(password);
  106. session.connect();
  107. Log.log("Connected");
  108.  
  109. Channel channel = session.openChannel("exec");
  110. ((ChannelExec) channel).setCommand(command1);
  111. channel.setInputStream(null);
  112. ((ChannelExec) channel).setErrStream(System.err);
  113. InputStream inp = channel.getInputStream();
  114. channel.connect();
  115. byte[] tmp = new byte[1024];
  116. while (true)
  117. {
  118. while (inp.available() > 0)
  119. {
  120. int i = inp.read(tmp, 0, 1024);
  121. if (i < 0)
  122. break;
  123. Log.log(new String(tmp, 0, i));
  124. System.out.println(tmp.toString());
  125. }
  126. if (channel.isClosed())
  127. {
  128. Log.log("exit-status: " + channel.getExitStatus());
  129.  
  130. break;
  131. }
  132. try
  133. {
  134. Thread.sleep(500);
  135. }
  136. catch (Exception ee)
  137. {
  138.  
  139. }
  140. }
  141. channel.disconnect();
  142. session.disconnect();
  143. Log.log("Validation of the host completed " + host);
  144. Log.log("------------------------------DONE------------------------");
  145. }
  146. catch (Exception e)
  147. {
  148. e.printStackTrace();
  149.  
  150. }
  151. finally
  152. {
  153. if (input != null)
  154. {
  155. try
  156. {
  157. input.close();
  158. }
  159. catch (IOException e)
  160. {
  161. e.printStackTrace();
  162. }
  163. }
  164. }
  165. }
  166.  
  167. command=ls -lrt /appl/websphere.ear | ls -lrt /appl/prd*/ProcMgrA* | cat /appl/prd*/EF_info.txt
Add Comment
Please, Sign In to add comment