Advertisement
Guest User

Untitled

a guest
Mar 29th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. package com.petpace.infra.utils;
  2.  
  3.  
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.net.InetAddress;
  10.  
  11. import jsystem.extensions.analyzers.text.FindText;
  12. import jsystem.framework.report.ListenerstManager;
  13. import jsystem.framework.report.Reporter;
  14. import systemobject.terminal.Prompt;
  15.  
  16. import com.aqua.sysobj.conn.CliCommand;
  17. import com.aqua.sysobj.conn.CliConnectionImpl;
  18.  
  19. import ch.ethz.ssh2.Connection;
  20. import ch.ethz.ssh2.SCPClient;
  21.  
  22. /**
  23. * @author Itai.Agmon
  24. *
  25. */
  26. public class CopyUtils {
  27.  
  28. /**
  29. * Maximum number of times we will try to send or receive the file in case
  30. * of error.
  31. */
  32. private static final int MAX_RETRIES = 3;
  33.  
  34. /**
  35. * We would like to report any problems.
  36. */
  37. private static Reporter report = ListenerstManager.getInstance();
  38.  
  39. /**
  40. * The number of times we tried to send or receive the file.
  41. */
  42. private static int connCounter = 0;
  43.  
  44. /**
  45. * Send file using SCP (SSH copy). This is used only on Linux machines. Will
  46. * try to send the file. In case of error it will send again until exceeded
  47. * maximum number of retries.
  48. *
  49. * @param hostName
  50. * The destination machine name or ip
  51. * @param user
  52. * @param password
  53. * @param fileName
  54. * @param destination
  55. * @param pemFile
  56. * @throws IOException
  57. * In case of problem connecting the remote machine.<br>
  58. * or if failed to authenticate <br>
  59. * or failed to send the file mor the MAX_RETRIES times.
  60. */
  61. public static void scpPut(final String hostName, final String user, final String password, final String fileName,
  62. final String destination, File pemFile) throws IOException {
  63. Connection conn = new Connection(hostName);
  64. conn.connect();
  65. conn.authenticateWithPublicKey(user, pemFile, password);
  66. if (conn.isAuthenticationComplete() == false) {
  67. throw new IOException("Authentication failed," + "parameters used: hostName=" + hostName + ", user=" + user
  68. + ", password=" + password);
  69. }
  70. SCPClient scp = conn.createSCPClient();
  71. try {
  72. scp.put(fileName, destination, "0777");
  73. } catch (IOException e) {
  74. report.report("Failed to send file " + fileName + " to " + user + "@" + hostName + ":" + destination
  75. + ". trying again", 2);
  76. if (connCounter++ < MAX_RETRIES) {
  77. try {
  78. Thread.sleep(100);
  79. } catch (InterruptedException e1) {
  80. // I don't really care.
  81. e1.printStackTrace();
  82. }
  83. conn.close();
  84. scpPut(hostName, user, password, fileName, destination, pemFile);
  85. } else {
  86. // In case we got exceptions more then the maximum retries
  87. // allowed, we initialize the connection counter and throw the
  88. // exception.
  89. connCounter = 0;
  90. throw e;
  91. }
  92. } finally {
  93. conn.close();
  94. }
  95. // In case we succeeded in sending the file we need to make sure that
  96. // the
  97. // connection counter is initialized.
  98. connCounter = 0;
  99. }
  100.  
  101. /**
  102. *
  103. * @param hostName
  104. * @param user
  105. * @param password
  106. * @param remoteFileName
  107. * @param localTarget
  108. * @param pemFile
  109. * @throws IOException
  110. * @throws FileNotFoundException
  111. * Local target file was not found.
  112. *
  113. */
  114. public static void scpGet(String hostName, String user, String password, String remoteFileName, String localTarget, File pemFile)
  115. throws IOException, FileNotFoundException {
  116. Connection conn = new Connection(hostName);
  117. conn.connect();
  118. //conn.authenticateWithPublicKey(user, pemFile, password);
  119. conn.authenticateWithPassword(user, password);
  120. if (conn.isAuthenticationComplete() == false) {
  121. throw new IOException("Authentication failed," + "parameters used: hostName=" + hostName + ", user=" + user
  122. + ", password=" + password);
  123. }
  124. SCPClient scp = conn.createSCPClient();
  125. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localTarget));
  126.  
  127. try {
  128. scp.get(remoteFileName, bos);
  129. } catch (IOException e) {
  130. report.report("Failed to recevie file " + localTarget + " from " + user + "@" + hostName + ":"
  131. + remoteFileName + ". trying again", 2);
  132. if (connCounter++ < MAX_RETRIES) {
  133. try {
  134. Thread.sleep(100);
  135. } catch (InterruptedException e1) {
  136. // I don't really care.
  137. e1.printStackTrace();
  138. }
  139. scp.get(remoteFileName, bos);
  140. } else {
  141. // In case we got exceptions more then the maximum retries
  142. // allowed, we initialize the connection counter and throw the
  143. // exception.
  144. connCounter = 0;
  145. throw e;
  146.  
  147. }
  148.  
  149. } finally {
  150. bos.close();
  151. conn.close();
  152. }
  153. // In case we succeeded in sending the file we need to make sure that
  154. // the
  155. // connection counter is initialized.
  156. connCounter = 0;
  157.  
  158. }
  159.  
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement