ch.ethz.ssh2.SCPClient Java Examples

The following examples show how to use ch.ethz.ssh2.SCPClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScpService.java    From wecube-platform with Apache License 2.0 7 votes vote down vote up
public void getFile(String ip, Integer port, String user, String password, String privateKey, boolean usePassword, String remoteFile, String path) {
    Connection connection = new Connection(ip, port);
    try {
        connection.connect();
        boolean isAuthed = isAuth(ip, port, user, password, privateKey, usePassword);
        if (isAuthed) {
            System.out.println("Authentication is successful!");
            SCPClient scpClient = connection.createSCPClient();
            scpClient.get(remoteFile, path);
        } else {
            System.out.println("Authentication failed!");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        connection.close();
    }
}
 
Example #2
Source File: ScpService.java    From wecube-platform with Apache License 2.0 6 votes vote down vote up
public void putFile(String ip, Integer port, String user, String password, String privateKey, boolean usePassword, String localFile, String remoteTargetDirectory) {
    log.info("Start to connect {}:{}", ip, port);
    Connection connection = new Connection(ip, port);
    try {
        connection.connect();
        boolean isAuthed = isAuth(ip, port, user, password, privateKey, usePassword);
        if (isAuthed) {
            SCPClient scpClient = connection.createSCPClient();
            scpClient.put(localFile, remoteTargetDirectory);
        } else {
            System.out.println("Authentication failed!");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        connection.close();
    }
}
 
Example #3
Source File: ScpClientUtil.java    From eladmin with Apache License 2.0 6 votes vote down vote up
public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) {
	Connection conn = new Connection(ip, port);
	try {
		conn.connect();
		boolean isAuthenticated = conn.authenticateWithPassword(username, password);
		if (!isAuthenticated) {
			System.err.println("authentication failed");
		}
		SCPClient client = new SCPClient(conn);
		if ((mode == null) || (mode.length() == 0)) {
			mode = "0600";
		}
		if (remoteFileName == null) {
			client.put(localFile, remoteTargetDirectory);
		} else {
			client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
		}
	} catch (IOException ex) {
		Logger.getLogger(ScpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
	}finally{
		conn.close();
	}
}
 
Example #4
Source File: ScpClientUtil.java    From eladmin with Apache License 2.0 6 votes vote down vote up
public void getFile(String remoteFile, String localTargetDirectory) {
	Connection conn = new Connection(ip, port);
	try {
		conn.connect();
		boolean isAuthenticated = conn.authenticateWithPassword(username, password);
		if (!isAuthenticated) {
			System.err.println("authentication failed");
		}
		SCPClient client = new SCPClient(conn);
		client.get(remoteFile, localTargetDirectory);
	} catch (IOException ex) {
		Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
	}finally{
		conn.close();
	}
}
 
Example #5
Source File: SSHUtil.java    From EserKnife with Apache License 2.0 6 votes vote down vote up
/**
 * 将
 *
 * @param ip
 * @param port
 * @param username
 * @param password
 * @param localPath
 * @param remoteDir
 * @return
 */
public static boolean scpFileToRemote(String ip, int port, String username, String password, String localPath, String remoteDir) throws SSHException {
    boolean isSuccess = true;
    Connection connection = new Connection(ip, port);
    try {
        connection.connect();
        boolean isAuthed = connection.authenticateWithPassword(username, password);
        if (!isAuthed) {
            throw new SSHException("auth error.");
        }
        SCPClient scpClient = connection.createSCPClient();
        scpClient.put(localPath, remoteDir, "0644");
    } catch (IOException e) {
        isSuccess = false;
        throw new SSHException("scp file to remote error.", e);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
    return isSuccess;
}
 
Example #6
Source File: SSHExecutor.java    From zkdoctor with Apache License 2.0 6 votes vote down vote up
/**
 * 写远程文件
 *
 * @param remoteDir 远程文件路径
 * @param fileName  远程文件名称
 * @param content   文件内容
 * @return
 */
public Result writeRemoteFile(String remoteDir, String fileName, String content) {
    try {
        if (StringUtils.isNotBlank(content)) {
            // window下的回车为\r\n,而linux为\n,需要进行替换
            content = content.replace("\r\n", "\n");
            SCPClient client = conn.createSCPClient();
            // 创建的文件,用户拥有相关权限
            client.put(content.getBytes(), fileName, remoteDir, "0644");
            return new Result(true);
        }
    } catch (Exception e) {
        LOGGER.error("Write {} to remoteFile {}/{} error.", content, remoteDir, fileName, e);
        return new Result(e);
    }
    return new Result(false);
}
 
Example #7
Source File: ScpService.java    From wecube-platform with Apache License 2.0 6 votes vote down vote up
public void put(String ip, Integer port, String user, String password, String localFile, String remoteTargetDirectory) {
    Connection conn = new Connection(ip, port);
    try {
        conn.connect();
        boolean isconn = conn.authenticateWithPassword(user, password);
        if (isconn) {
            log.info("Connection is OK");
            SCPClient scpClient = conn.createSCPClient();
            log.info("scp local file [{}] to remote target directory [{}]", localFile, remoteTargetDirectory);
            scpClient.put(localFile, remoteTargetDirectory, "7777");
        } else {
            log.info("User or password incorrect");
            throw new WecubeCoreException("User or password incorrect");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new WecubeCoreException("Run 'scp' command meet error: " + e.getMessage());
    } finally {
        conn.close();
    }
}
 
Example #8
Source File: SSHExecutor.java    From zkdoctor with Apache License 2.0 5 votes vote down vote up
/**
 * 复制本地的文件至远程服务器
 *
 * @param remoteDir 远程服务器路径
 * @param localPath 本地文件路径
 * @return
 */
public Result scpFileToRemote(String remoteDir, String localPath) {
    try {
        SCPClient scpClient = conn.createSCPClient();
        scpClient.put(localPath, remoteDir, "0644");
        return new Result(true);
    } catch (Exception e) {
        LOGGER.error("Scp file {} to remote dir {} error.", localPath, remoteDir, e);
        return new Result(e);
    }
}
 
Example #9
Source File: SSHExecutor.java    From zkdoctor with Apache License 2.0 5 votes vote down vote up
/**
 * 获取远程文件内容
 *
 * @param remoteFile 远程文件名称,包含路径
 * @return
 */
public Result getRemoteFile(String remoteFile) {
    try {
        SCPClient client = conn.createSCPClient();
        OutputStream outputStream = new ByteArrayOutputStream();
        client.get(remoteFile, outputStream);
        return new Result(true, outputStream.toString());
    } catch (Exception e) {
        LOGGER.error("Get remoteFile {} error.", remoteFile, e);
        return new Result(e);
    }
}
 
Example #10
Source File: SshUtil.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public static String scp(String hostIp, String hostName,
                         String hostPassword, String localFile, String remotePath) throws Exception {
    Connection conn = createConn(hostIp, hostName, hostPassword);

    SCPClient scpClient = new SCPClient(conn);
    scpClient.put(localFile, remotePath, "0777");
    return "upLoad success";
}
 
Example #11
Source File: SSHExecutor.java    From PoseidonX with Apache License 2.0 5 votes vote down vote up
/**
 * 获取远程文件内容
 *
 * @param remoteFile 远程文件名称,包含路径
 * @return
 */
public Result getRemoteFile(String remoteFile) {
    try {
        SCPClient client = conn.createSCPClient();
        OutputStream outputStream = new ByteArrayOutputStream();
        client.get(remoteFile, outputStream);
        return new Result(true, outputStream.toString());
    } catch (Exception e) {
        LOGGER.error("Get remoteFile {} error.", remoteFile, e);
        return new Result(e);
    }
}
 
Example #12
Source File: SSHExecutor.java    From PoseidonX with Apache License 2.0 5 votes vote down vote up
/**
 * 写远程文件
 *
 * @param remoteDir 远程文件路径
 * @param fileName  远程文件名称
 * @param content   文件内容
 * @return
 */
public Result writeRemoteFile(String remoteDir, String fileName, String content) {
    try {
        if (StringUtils.isNotBlank(content)) {
            SCPClient client = conn.createSCPClient();
            client.put(content.getBytes(), fileName, remoteDir);
            return new Result(true);
        }
    } catch (Exception e) {
        LOGGER.error("Write {} to remoteFile {}/{} error.", content, remoteDir, fileName);
        return new Result(e);
    }
    return new Result(false);
}
 
Example #13
Source File: SSHExecutor.java    From PoseidonX with Apache License 2.0 5 votes vote down vote up
/**
 * 复制本地的文件至远程服务器
 *
 * @param remoteDir 远程服务器路径
 * @param localPath 本地文件路径
 * @return
 */
public Result scpFileToRemote(String remoteDir, String localPath) {
    try {
        SCPClient scpClient = conn.createSCPClient();
        scpClient.put(localPath, remoteDir, "0644");
        return new Result(true);
    } catch (Exception e) {
        LOGGER.error("scp file {} to remote dir {} error.", localPath, remoteDir);
        return new Result(e);
    }
}
 
Example #14
Source File: SSHTemplate.java    From javabase with Apache License 2.0 3 votes vote down vote up
/**
 	 * Copy a set of local files to a remote directory, uses the specified mode when
 	 * creating the file on the remote side.
 	 * @param localFiles
 	 *            Path and name of local file.
 	 * @param remoteFiles
 	 *            name of remote file.
 	 * @param remoteTargetDirectory
 	 *            Remote target directory. Use an empty string to specify the default directory.
 	 * @param mode
 	 *            a four digit string (e.g., 0644, see "man chmod", "man open")
 	 * @throws IOException
 	 */
 	public Result scp(String[] localFiles, String[] remoteFiles, String remoteTargetDirectory, String mode) {
 		try {
 			SCPClient client = conn.createSCPClient();
	client.put(localFiles, remoteFiles, remoteTargetDirectory, mode);
	return new Result(true);
} catch (Exception e) {
	logger.error("scp local="+ Arrays.toString(localFiles)+" to "+
			remoteTargetDirectory+" remote="+ Arrays.toString(remoteFiles)+" err", e);
	return new Result(e);
}
 	}
 
Example #15
Source File: SSHTemplate.java    From cachecloud with Apache License 2.0 3 votes vote down vote up
/**
 	 * Copy a set of local files to a remote directory, uses the specified mode when
 	 * creating the file on the remote side.
 	 * @param localFiles
 	 *            Path and name of local file.
 	 * @param remoteFiles
 	 *            name of remote file.
 	 * @param remoteTargetDirectory
 	 *            Remote target directory. Use an empty string to specify the default directory.
 	 * @param mode
 	 *            a four digit string (e.g., 0644, see "man chmod", "man open")
 	 * @throws IOException
 	 */
 	public Result scp(String[] localFiles, String[] remoteFiles, String remoteTargetDirectory, String mode) {
 		try {
 			SCPClient client = conn.createSCPClient();
	client.put(localFiles, remoteFiles, remoteTargetDirectory, mode);
	return new Result(true);
} catch (Exception e) {
	logger.error("scp local="+Arrays.toString(localFiles)+" to "+
			remoteTargetDirectory+" remote="+Arrays.toString(remoteFiles)+" err", e);
	return new Result(e);
}
 	}
 
Example #16
Source File: SSH2Util.java    From util with Apache License 2.0 2 votes vote down vote up
/**
 * 功能:使用ssh2 上传文件。
 * @author 朱志杰 QQ:862990787
 * Sep 2, 2013 10:03:40 AM
 * @param remoteTargetDirectory 上传到服务器上的文件夹路径。
 * @param localFile 将要上传本地文件的路径。
 * @throws IOException 
 */
public void uploadFile(String remoteTargetDirectory,String localFile) throws IOException{
	SCPClient scpClient = conn.createSCPClient();   
	scpClient.put(localFile, remoteTargetDirectory); //从本地复制文件到远程目录   
}
 
Example #17
Source File: SSH2Util.java    From util with Apache License 2.0 2 votes vote down vote up
/**
 * 功能:使用ssh2 下载文件。
 * @author 朱志杰 QQ:862990787
 * Sep 2, 2013 10:03:40 AM
 * @param remoteFile 服务器上的文件路径。
 * @param localTargetDirectory 下载到本地的文件夹路径。
 * @throws IOException 
 */
public void downloadFile(String remoteFile,String localTargetDirectory) throws IOException{
	SCPClient scpClient = conn.createSCPClient();
	scpClient.get(remoteFile,localTargetDirectory);  //从远程获取文件  
}