@Test public void shouldSCPUploadFile() throws IOException { SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); assertFalse(targetFile.exists()); assertTrue(targetDir.exists()); scpFileTransfer.upload(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); assertTrue(targetFile.exists()); }
@Test public void shouldSCPUploadFileWithBandwidthLimit() throws IOException { // Limit upload transfer at 2Mo/s SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer().bandwidthLimit(16000); assertFalse(targetFile.exists()); scpFileTransfer.upload(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); assertTrue(targetFile.exists()); }
@Override public SshResult run() { SshResult ret = new SshResult(); String cmd = getCommand(); ret.setCommandToExecute(cmd); try { ssh.newSCPFileTransfer().upload(src, dst); if (logger.isTraceEnabled()) { logger.trace(String.format("[SCP done]: %s", cmd)); } ret.setReturnCode(0); } catch (IOException e) { if (!suppressException) { logger.warn(String.format("[SCP failed]: %s", cmd), e); } ret.setSshFailure(true); ret.setReturnCode(1); ret.setExitErrorMessage(e.getMessage()); } return ret; }
) throws IOException { final byte[] bytes = content.getBytes(Charsets.UTF_8); client.newSCPFileTransfer().upload(new InMemorySourceFile() { @Override public String getName() {
@Override public Integer executeArtifact(String scriptContent, Map<String, Object> variables, OutputHandler outputHandler) { checkConnection(); Path tempFile = null; try { tempFile = Files.createTempFile("puccini", ""); ScalaFileUtil.writeTextFile(scriptContent, tempFile); String remoteFile = "/tmp/" + tempFile.getFileName().toString(); SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); scpFileTransfer.upload(tempFile.toString(), remoteFile); return executeRemoteArtifact(remoteFile, variables, outputHandler); } catch (IOException e) { throw new ArtifactIOException("Could not create temporary file for script content", e); } finally { if (tempFile != null) { try { ScalaFileUtil.delete(tempFile); } catch (Exception ignored) { } } } }
/** * Upload one or more files via the same SSH/SCP connection to a remote host. */ public static void scpUpload(HostInfo hostInfo, List<FromTo> fromTos) throws IOException { SSHClient ssh = getSshClient(hostInfo); try { Session session = ssh.startSession(); session.allocateDefaultPTY(); try { for (FromTo ft: fromTos) { System.out.format("SCP cp %s -> %s/%s%n", ft.from, hostInfo.host, ft.to); ssh.newSCPFileTransfer().upload(ft.from, ft.to); } } finally { session.close(); } } finally { ssh.disconnect(); ssh.close(); } }
@Override public void upload(String localPath, String remoteDirectory) { checkConnection(); try (SimpleCommandOutputHandler outputHandler = new SimpleCommandOutputHandler("Prepare Upload " + localPath, log)) { String prepareCommand = getPrepareUploadArtifactCommand(remoteDirectory); executeCommand(prepareCommand, outputHandler); SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); scpFileTransfer.upload(localPath, remoteDirectory); } catch (ConnectionException | TransportException e) { ExceptionUtil.checkInterrupted(e); throw new ArtifactConnectException("Upload [" + localPath + "] to [" + remoteDirectory + "] encountered connection error", e); } catch (IOException e) { ExceptionUtil.checkInterrupted(e); throw new ArtifactUploadException("Fatal error happened while trying to upload", e); } }