private void copyFile(String sourcePath, String absoluteTargetPath, ChannelSftp channelSftp) throws MachineException { try { channelSftp.put(sourcePath, absoluteTargetPath); // apply permissions File file = new File(sourcePath); // read int permissions = 256; // execute if (file.canExecute()) { permissions += 64; } // write if (file.canWrite()) { permissions += 128; } channelSftp.chmod(permissions, absoluteTargetPath); } catch (SftpException e) { throw new MachineException( format( "Sftp copying of file %s failed. Error: %s", absoluteTargetPath, e.getLocalizedMessage())); } }
public List list(String parent) throws IOException { try { ChannelSftp c = getSftpChannel(parent); String path = getPath(parent); Collection r = c.ls(path); if (r != null) { if (!path.endsWith("/")) { path = parent + "/"; } List result = new ArrayList(); for (Iterator iter = r.iterator(); iter.hasNext(); ) { Object obj = iter.next(); if (obj instanceof LsEntry) { LsEntry entry = (LsEntry) obj; if (".".equals(entry.getFilename()) || "..".equals(entry.getFilename())) { continue; } result.add(path + entry.getFilename()); } } return result; } } catch (SftpException e) { IOException ex = new IOException("Failed to return a listing for '" + parent + "'"); ex.initCause(e); throw ex; } catch (URISyntaxException usex) { IOException ex = new IOException("Failed to return a listing for '" + parent + "'"); ex.initCause(usex); throw ex; } return null; }
public InputStream openStream(SFTPResource resource) throws IOException { ChannelSftp c = getSftpChannel(resource.getName()); try { String path = getPath(resource.getName()); return c.get(path); } catch (SftpException e) { IOException ex = new IOException( "impossible to open stream for " + resource + " on " + getHost() + (e.getMessage() != null ? ": " + e.getMessage() : "")); ex.initCause(e); throw ex; } catch (URISyntaxException e) { IOException ex = new IOException( "impossible to open stream for " + resource + " on " + getHost() + (e.getMessage() != null ? ": " + e.getMessage() : "")); ex.initCause(e); throw ex; } }
public void get(String source, File destination) throws IOException { fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET); ChannelSftp c = getSftpChannel(source); try { String path = getPath(source); c.get(path, destination.getAbsolutePath(), new MyProgressMonitor()); } catch (SftpException e) { IOException ex = new IOException( "impossible to get " + source + " on " + getHost() + (e.getMessage() != null ? ": " + e.getMessage() : "")); ex.initCause(e); throw ex; } catch (URISyntaxException e) { IOException ex = new IOException( "impossible to get " + source + " on " + getHost() + (e.getMessage() != null ? ": " + e.getMessage() : "")); ex.initCause(e); throw ex; } }
private void mkdirs(String path) throws Exception { // sftp.cd("/"); //change to the root dir // String dirs[] = splitPath(path); String cwd = sftp.pwd(); // System.out.println("CWD: " + cwd); while (!path.startsWith(cwd)) { System.out.println(cwd + " " + path); sftp.cd(".."); // should throw exception if can't cdup System.out.println("CDUP!"); cwd = sftp.pwd(); } String mkPath = path.substring(cwd.length(), path.length()); System.out.println("DIRS TO MAKE: " + mkPath); String dirs[] = splitPath(mkPath); for (int i = 0; i < dirs.length; i++) { System.out.println("mkdir " + dirs[i]); logger.info("mkdir " + dirs[i]); // swallow exception that results from trying to // make a dir that already exists try { sftp.mkdir(dirs[i]); } catch (Exception ex) { } // change to the new dir // throws an exception if something went wrong sftp.cd(dirs[i]); } }
private static void sftpUpload() { JSch.setLogger(new JschLogger()); Session session = null; try { JSch jsch = new JSch(); session = jsch.getSession(USERNAME, HOST, PORT); session.setPassword(PASSWORD); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); Channel channel = session.openChannel(PROTOCOL_SFTP); channel.connect(); BufferedInputStream in = new BufferedInputStream(new FileInputStream(SOURCE)); ChannelSftp channelSftp = (ChannelSftp) channel; channelSftp.cd(TARGET); channelSftp.put(in, FILE_NAME); } catch (JSchException | SftpException | FileNotFoundException ex) { Logger.getLogger(JschDemo.class.getName()).log(Level.SEVERE, null, ex); } finally { if (session != null) { session.disconnect(); } } }
@Test public void testCreateClientFailsIfPwdReturnsRelativePath() throws Exception { final String remoteRoot = "some/directory/in/my/home/dir"; hostConfig = createWithOverrideUsernameAndPassword(mockJSch); getHostConfig().setRemoteRootDir(remoteRoot); final BapSshCommonConfiguration commonConfiguration = new BapSshCommonConfiguration("Ignore me", null, null, false); getHostConfig().setCommonConfig(commonConfiguration); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockSession.setPassword(TEST_PASSPHRASE); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); expect(mockSftp.pwd()).andReturn("home/bap/" + remoteRoot); expect(mockSftp.isConnected()).andReturn(false); expect(mockSession.isConnected()).andReturn(false); assertCreateClientThrowsException("home/bap/" + remoteRoot); }
@Test public void testCreateClientWillUseKeyIfKeyAndKeyPathPresent() throws Exception { final String testKey = "MyVeryBigKey"; final BapSshCommonConfiguration defaultKeyInfo = new BapSshCommonConfiguration( TEST_PASSPHRASE, testKey, "/this/file/will/not/be/used", false); hostConfig = createWithDefaultKeyInfo(mockJSch, defaultKeyInfo); getHostConfig().setPassword("Ignore me"); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockJSch.addIdentity( isA(String.class), aryEq(BapSshUtil.toBytes(testKey)), (byte[]) isNull(), aryEq(BapSshUtil.toBytes(TEST_PASSPHRASE))); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); assertCreateClient(); }
@Test public void testCreateClientWithOverrideKeyPath() throws Exception { final String testKeyFilename = "myPrivateKey"; final RandomFile theKey = new RandomFile(jenkinsHome.getRoot(), testKeyFilename); hostConfig = createWithOverrideUsernameAndPassword(mockJSch, TEST_PASSPHRASE, testKeyFilename, ""); final BapSshCommonConfiguration commonConfiguration = new BapSshCommonConfiguration("Ignore me", null, null, false); getHostConfig().setCommonConfig(commonConfiguration); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockJSch.addIdentity( isA(String.class), aryEq(theKey.getContents()), (byte[]) isNull(), aryEq(BapSshUtil.toBytes(TEST_PASSPHRASE))); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); assertCreateClient(); }
private BapSshClient assertCreateClientWithDefaultKey(final boolean disableExec) throws Exception { final String testKey = "MyVeryBigKey"; final BapSshCommonConfiguration defaultKeyInfo = new BapSshCommonConfiguration(TEST_PASSPHRASE, testKey, null, disableExec); hostConfig = createWithDefaultKeyInfo(mockJSch, defaultKeyInfo); getHostConfig().setPassword("Ignore me"); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockJSch.addIdentity( isA(String.class), aryEq(BapSshUtil.toBytes(testKey)), (byte[]) isNull(), aryEq(BapSshUtil.toBytes(defaultKeyInfo.getPassphrase()))); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); return assertCreateClient(); }
public void disconnect() throws GenericFileOperationFailedException { if (session != null && session.isConnected()) { session.disconnect(); } if (channel != null && channel.isConnected()) { channel.disconnect(); } }
public static void main(String[] args) { SFTPGetTest test = new SFTPGetTest(); Map<String, String> sftpDetails = new HashMap<String, String>(); // 设置主机ip,用户名,密码,端口 sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, IP); sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, PORT); sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, USERNAME); sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, PASSWOED); // 创建下载通道。以及超时时长,单位是毫秒 SFTPChannel channel = test.getSFTPChannel(); ChannelSftp chSftp = null; try { chSftp = channel.getChannel(sftpDetails, delayTime); } catch (JSchException e1) { System.out.println("error:创建下载通道失败!failed to creat a channel!"); e1.printStackTrace(); } SftpATTRS attr = null; try { attr = chSftp.stat(FILE_PATH_SFTP); } catch (SftpException e1) { System.out.println("error:获取文件大小失败失败!failed to get the size of the file!"); e1.printStackTrace(); } long fileSize = attr.getSize(); System.out.println("fileSize=" + fileSize); try { chSftp.get(FILE_PATH_SFTP, DESTINATION_LOCAL, new MyProgressMonitor(fileSize)); // 代码段1 // OutputStream out = new FileOutputStream(DESTINATION_LOCAL);//这是采用文件输入流方式下载文件 // chSftp.get(FILE_PATH_SFTP, out, new FileProgressMonitor(fileSize)); // 代码段2 /** * 代码段3 * * <p>OutputStream out = new FileOutputStream(DESTINATION_LOCAL);//这是采用文件输入流方式下载文件 InputStream * is = chSftp.get(FILE_PATH_SFTP, new MyProgressMonitor()); byte[] buff = new byte[1024 * 2]; * int read; if (is != null) { System.out.println("Start to read input stream"); do { read = * is.read(buff, 0, buff.length); if (read > 0) { out.write(buff, 0, read); } out.flush(); } * while (read >= 0); System.out.println("input stream read done."); } */ } catch (Exception e) { e.printStackTrace(); } finally { chSftp.quit(); try { channel.closeChannel(); } catch (Exception e) { System.out.println("error:关闭下载通道失败!failed to close the channel!"); e.printStackTrace(); } } }
private void copyRecursively(String sourceFolder, String targetFolder) throws MachineException { // create target dir try { int execCode = execAndGetCode("mkdir -p " + targetFolder); if (execCode != 0) { throw new MachineException( format("Creation of folder %s failed. Exit code is %s", targetFolder, execCode)); } } catch (JSchException | IOException e) { throw new MachineException( format("Creation of folder %s failed. Error: %s", targetFolder, e.getLocalizedMessage())); } // not normalized paths don't work final String targetAbsolutePath = getAbsolutePath(targetFolder); // copy files ChannelSftp sftp = null; try { sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(connectionTimeout); final ChannelSftp finalSftp = sftp; Files.walkFileTree( Paths.get(sourceFolder), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { try { if (!attrs.isDirectory()) { copyFile( file.toString(), Paths.get(targetAbsolutePath, file.getFileName().toString()).toString(), finalSftp); } else { finalSftp.mkdir(file.normalize().toString()); } } catch (MachineException | SftpException e) { throw new IOException( format( "Sftp copying of file %s failed. Error: %s", file, e.getLocalizedMessage())); } return FileVisitResult.CONTINUE; } }); } catch (JSchException | IOException e) { throw new MachineException("Copying failed. Error: " + e.getLocalizedMessage()); } finally { if (sftp != null) { sftp.disconnect(); } } }
/* (non-Javadoc) * @see net.sf.thingamablog.transport.PublishTransport#publishFile(java.lang.String, java.io.File, net.sf.thingamablog.transport.TransportProgress) */ public boolean publishFile(String pubPath, File file, TransportProgress tp) { if (sftp == null) { failMsg = "SFTP Client not initialized!"; return false; } if (!isConnected()) { failMsg = "Not Connected!!!"; return false; } if (tp.isAborted()) { failMsg = "Aborted"; return false; } if (!pubPath.endsWith("/")) pubPath += "/"; // append a trailing slash if needed try { String cwd = sftp.pwd(); if (!cwd.endsWith("/")) cwd += "/"; if (!pubPath.equals(cwd)) { boolean changedDir = false; try { sftp.cd(pubPath); // try to change to the pub path changedDir = true; // changed dir OK System.out.println("Changed to " + pubPath); } catch (Exception cdEx) { logger.log(Level.WARNING, "Problem changing SFTP dir", cdEx); } if (!changedDir) { // was unable to change dir. the dir likely does not exist // so we'll try making the dir structure of pubPath mkdirs(pubPath); // sftp.cd(pubPath); } } int mode = ChannelSftp.OVERWRITE; // String dest = pubPath + file.getName(); InputStream is = new FileInputStream(file); sftp.put(is, file.getName(), new MyProgressMonitor(tp), mode); is.close(); return true; } catch (Exception ex) { failMsg = "Error publishing file to " + pubPath; failMsg += "\n" + ex.getMessage(); logger.log(Level.WARNING, failMsg, ex); ex.printStackTrace(); } return false; }
public Payload get(String path) { checkNotNull(path, "path"); ChannelSftp sftp = getSftp(); try { return Payloads.newInputStreamPayload( new CloseFtpChannelOnCloseInputStream(sftp.get(path), sftp)); } catch (SftpException e) { throw new SshException( String.format("%s@%s:%d: Error getting path: %s", username, host, port, path), e); } }
@Test(groups = "spring") public void testDelete() throws SftpException { final Iterable<?> iterable = sftpChannel.ls("."); for (Object object : iterable) { com.jcraft.jsch.ChannelSftp.LsEntry entry = (LsEntry) object; System.out.println(entry.getFilename()); } // assert sftpChannel.lstat("to_delete") == null; sftpChannel.put(new ByteArrayInputStream("Hello World!".getBytes()), "to_delete"); assert sftpChannel.lstat("to_delete") != null; sftpChannel.rm("to_delete"); // Will throw exception if fails }
@Override public void send(String path, String filename, Binary content) throws IOException { Session session = null; Channel channel = null; ChannelSftp channelSftp = null; logger.debug("preparing the host information for sftp."); InputStream data = null; try { JSch jsch = new JSch(); session = jsch.getSession(this.username, this.server, this.remotePort); if (this.password != null) { session.setPassword(this.password); } java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); logger.debug("Host connected."); channel = session.openChannel("sftp"); channel.connect(); logger.debug("sftp channel opened and connected."); channelSftp = (ChannelSftp) channel; if (path != null) { channelSftp.cd(path); } File f = new File(filename); data = content.getDataAsStream(); channelSftp.put(data, f.getName()); logger.info("File transfered successfully to host."); } catch (Exception ex) { throw new IOException("SFTP problem", ex); } finally { if (data != null) { try { data.close(); } catch (IOException e) { } } if (channelSftp != null) { channelSftp.exit(); } logger.info("sftp Channel exited."); if (channel != null) { channel.disconnect(); } logger.info("Channel disconnected."); if (session != null) { session.disconnect(); } logger.info("Host Session disconnected."); } }
private void setLastModified(File localFile) throws JSchException { SftpATTRS fileAttributes = null; String remotePath = null; ChannelSftp channel = openSftpChannel(); channel.connect(); try { fileAttributes = channel.lstat(remoteDir(remoteFile) + localFile.getName()); } catch (SftpException e) { throw new JSchException("failed to stat remote file", e); } FileUtils.getFileUtils() .setFileLastModified(localFile, ((long) fileAttributes.getMTime()) * 1000); }
private ChannelSftp getSftp() { checkConnected(); logger.debug("%s@%s:%d: Opening sftp Channel.", username, host, port); ChannelSftp sftp = null; try { sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(); } catch (JSchException e) { throw new SshException( String.format("%s@%s:%d: Error connecting to sftp.", username, host, port), e); } return sftp; }
@Override public void put(String path, Payload contents) { checkNotNull(path, "path"); checkNotNull(contents, "contents"); ChannelSftp sftp = getSftp(); try { sftp.put(contents.getInput(), path); } catch (SftpException e) { throw new SshException( String.format("%s@%s:%d: Error putting path: %s", username, host, port, path), e); } finally { Closeables.closeQuietly(contents); } }
private void mkdirs(String directory, ChannelSftp c) throws IOException, SftpException { try { SftpATTRS att = c.stat(directory); if (att != null) { if (att.isDir()) { return; } } } catch (SftpException ex) { if (directory.indexOf('/') != -1) { mkdirs(directory.substring(0, directory.lastIndexOf('/')), c); } c.mkdir(directory); } }
private void copyFile(String sourcePath, String targetPath) throws MachineException { ChannelSftp sftp = null; try { sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(connectionTimeout); String absoluteTargetPath = getAbsolutePath(targetPath); copyFile(sourcePath, absoluteTargetPath, sftp); } catch (JSchException e) { throw new MachineException("Sftp copying failed. Error: " + e.getLocalizedMessage()); } finally { if (sftp != null) { sftp.disconnect(); } } }
private boolean buildDirectoryChunks(String dirName) throws IOException, SftpException { final StringBuilder sb = new StringBuilder(dirName.length()); final String[] dirs = dirName.split("/|\\\\"); boolean success = false; for (String dir : dirs) { sb.append(dir).append('/'); // must normalize the directory name String directory = endpoint.getConfiguration().normalizePath(sb.toString()); // do not try to build root folder (/ or \) if (!(directory.equals("/") || directory.equals("\\"))) { try { LOG.trace("Trying to build remote directory by chunk: {}", directory); channel.mkdir(directory); success = true; } catch (SftpException e) { // ignore keep trying to create the rest of the path } } } return success; }
public boolean isLoggedIn() { return isConnected() && session != null && session.isConnected() && channel != null && channel.isConnected(); }
/** Creates this file as a folder. */ public void createFolder(String foldername) throws KettleJobException { try { c.mkdir(foldername); } catch (SftpException e) { throw new KettleJobException(e); } }
public void delete(String file) throws KettleJobException { try { c.rm(file); } catch (SftpException e) { throw new KettleJobException(e); } }
public String pwd() throws KettleJobException { try { return c.pwd(); } catch (SftpException e) { throw new KettleJobException(e); } }
public String[] dir() throws KettleJobException { String[] fileList = null; try { java.util.Vector<?> v = c.ls("."); java.util.Vector<String> o = new java.util.Vector<String>(); if (v != null) { for (int i = 0; i < v.size(); i++) { Object obj = v.elementAt(i); if (obj != null && obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) { LsEntry lse = (com.jcraft.jsch.ChannelSftp.LsEntry) obj; if (!lse.getAttrs().isDir()) { o.add(lse.getFilename()); } } } } if (o.size() > 0) { fileList = new String[o.size()]; o.copyInto(fileList); } } catch (SftpException e) { throw new KettleJobException(e); } return fileList; }
public void chdir(String dirToChangeTo) throws KettleJobException { try { c.cd(dirToChangeTo); } catch (SftpException e) { throw new KettleJobException(e); } }
static void downloadAllFiles(String fileType) { try { java.util.Vector fileList = sftpChannel.ls("."); if (fileList != null) { for (int ii = 0; ii < fileList.size(); ii++) { Object obj = fileList.elementAt(ii); LsEntry lsentry = (com.jcraft.jsch.ChannelSftp.LsEntry) obj; if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) { String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getFilename(); if (fileType.equals(".*")) { if (!(lsentry.getAttrs().isDir())) { boolean valid = checkFileName(fileName); if (valid) { totalFileCount++; downloadFileByName(fileName); } } } else if (fileName.toLowerCase().endsWith(fileType)) { boolean valid = checkFileName(fileName); if (valid) { totalFileCount++; downloadFileByName(fileName); } } } } } } catch (Exception e) { System.out.println("Exception in downloadAllFiles(): " + e.toString()); errorMessage += "Exception in downloadAllFiles(): " + e.toString() + "\n"; } }