Java Code Examples for ch.ethz.ssh2.SCPClient#put()

The following examples show how to use ch.ethz.ssh2.SCPClient#put() . 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 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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); //从本地复制文件到远程目录   
}