@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 shouldSCPDownloadFile() throws IOException { SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); assertFalse(targetFile.exists()); scpFileTransfer.download(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()); }
@Test public void shouldSCPDownloadFileWithBandwidthLimit() throws IOException { // Limit download transfer at 128Ko/s SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer().bandwidthLimit(1024); assertFalse(targetFile.exists()); scpFileTransfer.download(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); assertTrue(targetFile.exists()); }
@Test public void shouldSCPDownloadFileWithoutPathEscaping() throws IOException { SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); assertFalse(targetFile.exists()); File file = tempFolder.newFile("new file.txt"); FileUtil.writeToFile(file, "Some content"); scpFileTransfer.download(tempFolder.getRoot().getAbsolutePath() + "/new file.txt", targetFile.getAbsolutePath()); assertTrue(targetFile.exists()); assertThat(FileUtil.readFromFile(targetFile), CoreMatchers.containsString("Some content")); } }
@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; }
SSHClient client = null; SCPUploadClient uploader = null; try { client = getClient(); uploader = client.newSCPFileTransfer().newSCPUploadClient(); uploader.setUploadFilter(new WildcardFileFilter(wildCardPattern)); //determine the remote directory File f = new File(localDirLocation); String dir = remoteDirLocation + f.getName(); uploader.copy(new FileSystemFile(localDirLocation), remoteDirLocation); } catch (IOException e) { //processing exceptions here } finally { disconnectClient(client); }
) throws IOException { final byte[] bytes = content.getBytes(Charsets.UTF_8); client.newSCPFileTransfer().upload(new InMemorySourceFile() { @Override public String getName() {
public void transfertFile(String remoteFile, File localFile) throws IOException { ssh.newSCPFileTransfer().download(remoteFile, new FileSystemFile(localFile)); }
@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(); } }
public Map<String, ?> processOutput(InvocationContext context) throws ProviderException { ApplicationDeploymentDescriptionType app = context.getExecutionDescription().getApp().getType(); try { // Get the Stdouts and StdErrs String timeStampedServiceName = GfacUtils.createUniqueNameForService(context.getServiceName()); File localStdOutFile = File.createTempFile(timeStampedServiceName, "stdout"); File localStdErrFile = File.createTempFile(timeStampedServiceName, "stderr"); SCPFileTransfer fileTransfer = ssh.newSCPFileTransfer(); fileTransfer.download(app.getStandardOutput(), localStdOutFile.getAbsolutePath()); fileTransfer.download(app.getStandardError(), localStdErrFile.getAbsolutePath()); String stdOutStr = GfacUtils.readFileToString(localStdOutFile.getAbsolutePath()); String stdErrStr = GfacUtils.readFileToString(localStdErrFile.getAbsolutePath()); return OutputUtils.fillOutputFromStdout(context.<ActualParameter> getOutput(), stdOutStr); } catch (XmlException e) { throw new ProviderException("Cannot read output:" + e.getMessage(), e); } catch (ConnectionException e) { throw new ProviderException(e.getMessage(), e); } catch (TransportException e) { throw new ProviderException(e.getMessage(), e); } catch (IOException e) { throw new ProviderException(e.getMessage(), e); } }
import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.xfer.FileSystemFile; import java.io.File; import java.io.IOException; /** This example demonstrates uploading of a file over SCP to the SSH server. */ public class SCPUpload { public static void main(String[] args) throws IOException, ClassNotFoundException { SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("localhost"); try { ssh.authPublickey("/path/to/key.pem")); // Present here to demo algorithm renegotiation - could have just put this before connect() // Make sure JZlib is in classpath for this to work ssh.useCompression(); final String src = System.getProperty("user.home") + File.separator + "test_file"; ssh.newSCPFileTransfer().upload(new FileSystemFile(src), "/tmp/"); } finally { ssh.disconnect(); } } }
public static void scpDownload(HostInfo hostInfo, FromTo fromTo) throws IOException { SSHClient ssh = getSshClient(hostInfo); try { Session session = ssh.startSession(); session.allocateDefaultPTY(); try { ssh.newSCPFileTransfer().download(fromTo.from, fromTo.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); } }