@Test public void testScp() throws Exception { if (isRemote == false) return; SSHClient ssh = new SSHClient(this.hostname, this.port, this.username, this.password); File tmpFile = File.createTempFile("test_scp", "", new File("/tmp")); tmpFile.deleteOnExit(); FileUtils.write(tmpFile, "test_scp", Charset.defaultCharset()); ssh.scpFileToRemote(tmpFile.getAbsolutePath(), "/tmp"); }
public void scpFileToRemote(String localFile, String remoteTargetDirectory) throws Exception { FileInputStream fis = null; try { logger.info("SCP file " + localFile + " to " + remoteTargetDirectory); Session session = newJSchSession(); session.connect(); boolean ptimestamp = false; String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteTargetDirectory; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); if (checkAck(in) != 0) { System.exit(0); } File _lfile = new File(localFile); if (ptimestamp) { command = "T " + (_lfile.lastModified() / 1000) + " 0"; command += (" " + (_lfile.lastModified() / 1000) + " 0\n"); out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } } long filesize = _lfile.length(); command = "C0644 " + filesize + " "; if (localFile.lastIndexOf("/") > 0) { command += localFile.substring(localFile.lastIndexOf("/") + 1); } else if (localFile.lastIndexOf(File.separator) > 0) { command += localFile.substring(localFile.lastIndexOf(File.separator) + 1); } else { command += localFile; } command += "\n"; out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } fis = new FileInputStream(localFile); byte[] buf = new byte[1024]; while (true) { int len = fis.read(buf, 0, buf.length); if (len <= 0) break; out.write(buf, 0, len); } fis.close(); fis = null; buf[0] = 0; out.write(buf, 0, 1); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } out.close(); channel.disconnect(); session.disconnect(); } catch (Exception e) { throw e; } finally { IOUtils.closeQuietly(fis); } }
SSHClient { public void scpFileToRemote(String localFile, String remoteTargetDirectory) throws Exception { FileInputStream fis = null; try { logger.info("SCP file " + localFile + " to " + remoteTargetDirectory); Session session = newJSchSession(); session.connect(); boolean ptimestamp = false; String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteTargetDirectory; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); if (checkAck(in) != 0) { System.exit(0); } File _lfile = new File(localFile); if (ptimestamp) { command = "T " + (_lfile.lastModified() / 1000) + " 0"; command += (" " + (_lfile.lastModified() / 1000) + " 0\n"); out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } } long filesize = _lfile.length(); command = "C0644 " + filesize + " "; if (localFile.lastIndexOf("/") > 0) { command += localFile.substring(localFile.lastIndexOf("/") + 1); } else if (localFile.lastIndexOf(File.separator) > 0) { command += localFile.substring(localFile.lastIndexOf(File.separator) + 1); } else { command += localFile; } command += "\n"; out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } fis = new FileInputStream(localFile); byte[] buf = new byte[1024]; while (true) { int len = fis.read(buf, 0, buf.length); if (len <= 0) break; out.write(buf, 0, len); } fis.close(); fis = null; buf[0] = 0; out.write(buf, 0, 1); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } out.close(); channel.disconnect(); session.disconnect(); } catch (Exception e) { throw e; } finally { IOUtils.closeQuietly(fis); } } }
SSHClient { public void scpFileToRemote(String localFile, String remoteTargetDirectory) throws Exception { FileInputStream fis = null; try { logger.info("SCP file " + localFile + " to " + remoteTargetDirectory); Session session = newJSchSession(); session.connect(); boolean ptimestamp = false; String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteTargetDirectory; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); if (checkAck(in) != 0) { System.exit(0); } File _lfile = new File(localFile); if (ptimestamp) { command = "T " + (_lfile.lastModified() / 1000) + " 0"; command += (" " + (_lfile.lastModified() / 1000) + " 0\n"); out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } } long filesize = _lfile.length(); command = "C0644 " + filesize + " "; if (localFile.lastIndexOf("/") > 0) { command += localFile.substring(localFile.lastIndexOf("/") + 1); } else if (localFile.lastIndexOf(File.separator) > 0) { command += localFile.substring(localFile.lastIndexOf(File.separator) + 1); } else { command += localFile; } command += "\n"; out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } fis = new FileInputStream(localFile); byte[] buf = new byte[1024]; while (true) { int len = fis.read(buf, 0, buf.length); if (len <= 0) break; out.write(buf, 0, len); } fis.close(); fis = null; buf[0] = 0; out.write(buf, 0, 1); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } out.close(); channel.disconnect(); session.disconnect(); } catch (Exception e) { throw e; } finally { IOUtils.closeQuietly(fis); } } SSHClient(String hostname, int port, String username, String password); }
SSHClient { public void scpFileToRemote(String localFile, String remoteTargetDirectory) throws Exception { FileInputStream fis = null; try { logger.info("SCP file " + localFile + " to " + remoteTargetDirectory); Session session = newJSchSession(); session.connect(); boolean ptimestamp = false; String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteTargetDirectory; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); if (checkAck(in) != 0) { System.exit(0); } File _lfile = new File(localFile); if (ptimestamp) { command = "T " + (_lfile.lastModified() / 1000) + " 0"; command += (" " + (_lfile.lastModified() / 1000) + " 0\n"); out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } } long filesize = _lfile.length(); command = "C0644 " + filesize + " "; if (localFile.lastIndexOf("/") > 0) { command += localFile.substring(localFile.lastIndexOf("/") + 1); } else if (localFile.lastIndexOf(File.separator) > 0) { command += localFile.substring(localFile.lastIndexOf(File.separator) + 1); } else { command += localFile; } command += "\n"; out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } fis = new FileInputStream(localFile); byte[] buf = new byte[1024]; while (true) { int len = fis.read(buf, 0, buf.length); if (len <= 0) break; out.write(buf, 0, len); } fis.close(); fis = null; buf[0] = 0; out.write(buf, 0, 1); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } out.close(); channel.disconnect(); session.disconnect(); } catch (Exception e) { throw e; } finally { IOUtils.closeQuietly(fis); } } SSHClient(String hostname, int port, String username, String password); void scpFileToRemote(String localFile, String remoteTargetDirectory); void scpFileToLocal(String rfile, String lfile); SSHClientOutput execCommand(String command); SSHClientOutput execCommand(String command, int timeoutSeconds, Logger logAppender); }
SSHClient { public void scpFileToRemote(String localFile, String remoteTargetDirectory) throws Exception { FileInputStream fis = null; try { logger.info("SCP file " + localFile + " to " + remoteTargetDirectory); Session session = newJSchSession(); session.connect(); boolean ptimestamp = false; String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteTargetDirectory; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); if (checkAck(in) != 0) { System.exit(0); } File _lfile = new File(localFile); if (ptimestamp) { command = "T " + (_lfile.lastModified() / 1000) + " 0"; command += (" " + (_lfile.lastModified() / 1000) + " 0\n"); out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } } long filesize = _lfile.length(); command = "C0644 " + filesize + " "; if (localFile.lastIndexOf("/") > 0) { command += localFile.substring(localFile.lastIndexOf("/") + 1); } else if (localFile.lastIndexOf(File.separator) > 0) { command += localFile.substring(localFile.lastIndexOf(File.separator) + 1); } else { command += localFile; } command += "\n"; out.write(command.getBytes(StandardCharsets.UTF_8)); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } fis = new FileInputStream(localFile); byte[] buf = new byte[1024]; while (true) { int len = fis.read(buf, 0, buf.length); if (len <= 0) break; out.write(buf, 0, len); } fis.close(); fis = null; buf[0] = 0; out.write(buf, 0, 1); out.flush(); if (checkAck(in) != 0) { throw new Exception(ERROR_IN_CHECK_ACK); } out.close(); channel.disconnect(); session.disconnect(); } catch (Exception e) { throw e; } finally { IOUtils.closeQuietly(fis); } } SSHClient(String hostname, int port, String username, String password); void scpFileToRemote(String localFile, String remoteTargetDirectory); void scpFileToLocal(String rfile, String lfile); SSHClientOutput execCommand(String command); SSHClientOutput execCommand(String command, int timeoutSeconds, Logger logAppender);  }