Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.petpace.infra.utils;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.net.InetAddress;
- import jsystem.extensions.analyzers.text.FindText;
- import jsystem.framework.report.ListenerstManager;
- import jsystem.framework.report.Reporter;
- import systemobject.terminal.Prompt;
- import com.aqua.sysobj.conn.CliCommand;
- import com.aqua.sysobj.conn.CliConnectionImpl;
- import ch.ethz.ssh2.Connection;
- import ch.ethz.ssh2.SCPClient;
- /**
- * @author Itai.Agmon
- *
- */
- public class CopyUtils {
- /**
- * Maximum number of times we will try to send or receive the file in case
- * of error.
- */
- private static final int MAX_RETRIES = 3;
- /**
- * We would like to report any problems.
- */
- private static Reporter report = ListenerstManager.getInstance();
- /**
- * The number of times we tried to send or receive the file.
- */
- private static int connCounter = 0;
- /**
- * Send file using SCP (SSH copy). This is used only on Linux machines. Will
- * try to send the file. In case of error it will send again until exceeded
- * maximum number of retries.
- *
- * @param hostName
- * The destination machine name or ip
- * @param user
- * @param password
- * @param fileName
- * @param destination
- * @param pemFile
- * @throws IOException
- * In case of problem connecting the remote machine.<br>
- * or if failed to authenticate <br>
- * or failed to send the file mor the MAX_RETRIES times.
- */
- public static void scpPut(final String hostName, final String user, final String password, final String fileName,
- final String destination, File pemFile) throws IOException {
- Connection conn = new Connection(hostName);
- conn.connect();
- conn.authenticateWithPublicKey(user, pemFile, password);
- if (conn.isAuthenticationComplete() == false) {
- throw new IOException("Authentication failed," + "parameters used: hostName=" + hostName + ", user=" + user
- + ", password=" + password);
- }
- SCPClient scp = conn.createSCPClient();
- try {
- scp.put(fileName, destination, "0777");
- } catch (IOException e) {
- report.report("Failed to send file " + fileName + " to " + user + "@" + hostName + ":" + destination
- + ". trying again", 2);
- if (connCounter++ < MAX_RETRIES) {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e1) {
- // I don't really care.
- e1.printStackTrace();
- }
- conn.close();
- scpPut(hostName, user, password, fileName, destination, pemFile);
- } else {
- // In case we got exceptions more then the maximum retries
- // allowed, we initialize the connection counter and throw the
- // exception.
- connCounter = 0;
- throw e;
- }
- } finally {
- conn.close();
- }
- // In case we succeeded in sending the file we need to make sure that
- // the
- // connection counter is initialized.
- connCounter = 0;
- }
- /**
- *
- * @param hostName
- * @param user
- * @param password
- * @param remoteFileName
- * @param localTarget
- * @param pemFile
- * @throws IOException
- * @throws FileNotFoundException
- * Local target file was not found.
- *
- */
- public static void scpGet(String hostName, String user, String password, String remoteFileName, String localTarget, File pemFile)
- throws IOException, FileNotFoundException {
- Connection conn = new Connection(hostName);
- conn.connect();
- //conn.authenticateWithPublicKey(user, pemFile, password);
- conn.authenticateWithPassword(user, password);
- if (conn.isAuthenticationComplete() == false) {
- throw new IOException("Authentication failed," + "parameters used: hostName=" + hostName + ", user=" + user
- + ", password=" + password);
- }
- SCPClient scp = conn.createSCPClient();
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localTarget));
- try {
- scp.get(remoteFileName, bos);
- } catch (IOException e) {
- report.report("Failed to recevie file " + localTarget + " from " + user + "@" + hostName + ":"
- + remoteFileName + ". trying again", 2);
- if (connCounter++ < MAX_RETRIES) {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e1) {
- // I don't really care.
- e1.printStackTrace();
- }
- scp.get(remoteFileName, bos);
- } else {
- // In case we got exceptions more then the maximum retries
- // allowed, we initialize the connection counter and throw the
- // exception.
- connCounter = 0;
- throw e;
- }
- } finally {
- bos.close();
- conn.close();
- }
- // In case we succeeded in sending the file we need to make sure that
- // the
- // connection counter is initialized.
- connCounter = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement