Java Code Examples for com.jcraft.jsch.ChannelSftp#disconnect()
The following examples show how to use
com.jcraft.jsch.ChannelSftp#disconnect() .
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: SSHShell.java From azure-libraries-for-java with MIT License | 7 votes |
/** * Creates a new file on the remote host using the input content. * * @param from the byte array content to be uploaded * @param fileName the name of the file for which the content will be saved into * @param toPath the path of the file for which the content will be saved into * @param isUserHomeBased true if the path of the file is relative to the user's home directory * @param filePerm file permissions to be set * @throws Exception exception thrown */ public void upload(InputStream from, String fileName, String toPath, boolean isUserHomeBased, String filePerm) throws Exception { ChannelSftp channel = (ChannelSftp) this.session.openChannel("sftp"); channel.connect(); String absolutePath = isUserHomeBased ? channel.getHome() + "/" + toPath : toPath; String path = ""; for (String dir : absolutePath.split("/")) { path = path + "/" + dir; try { channel.mkdir(path); } catch (Exception ee) { } } channel.cd(absolutePath); channel.put(from, fileName); if (filePerm != null) { channel.chmod(Integer.parseInt(filePerm), absolutePath + "/" + fileName); } channel.disconnect(); }
Example 2
Source File: SSHShell.java From azure-libraries-for-java with MIT License | 6 votes |
/** * Downloads the content of a file from the remote host as a String. * * @param fileName the name of the file for which the content will be downloaded * @param fromPath the path of the file for which the content will be downloaded * @param isUserHomeBased true if the path of the file is relative to the user's home directory * @return the content of the file * @throws Exception exception thrown */ public String download(String fileName, String fromPath, boolean isUserHomeBased) throws Exception { ChannelSftp channel = (ChannelSftp) this.session.openChannel("sftp"); channel.connect(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedOutputStream buff = new BufferedOutputStream(outputStream); String absolutePath = isUserHomeBased ? channel.getHome() + "/" + fromPath : fromPath; channel.cd(absolutePath); channel.get(fileName, buff); channel.disconnect(); return outputStream.toString(); }
Example 3
Source File: JobConfigUtil.java From jumbune with GNU Lesser General Public License v3.0 | 6 votes |
public static void sendJars(JumbuneRequest jumbuneRequest, String[] jarsPath) throws Exception { JobConfig jobConfig = jumbuneRequest.getJobConfig(); Cluster cluster = jumbuneRequest.getCluster(); String privateKeyPath = null; String password; String username; username = cluster.getAgents().getUser(); password = cluster.getAgents().getPassword(); if ((password != null) && (!password.trim().isEmpty())) { password = StringUtil.getPlain(cluster.getAgents().getPassword()); } else { privateKeyPath = cluster.getAgents().getSshAuthKeysFile(); } Session session = getSession(cluster.getNameNode(), username, password, privateKeyPath); ChannelSftp sftp = getChannel(session); String remoteDir = jumbuneRequest.getJobConfig().getTempDirectory() + jobConfig.getJumbuneJobName() + "/lib/"; createPathInRemote(sftp, new File(remoteDir)); for (String localJarPath : jarsPath) { String remoteFilePath = remoteDir + "/" + localJarPath.substring(localJarPath.lastIndexOf("/") + 1); sftp.put(new FileInputStream(localJarPath), remoteFilePath, 0); } sftp.disconnect(); session.disconnect(); }
Example 4
Source File: SftpFsHelper.java From incubator-gobblin with Apache License 2.0 | 6 votes |
@Override public long getFileMTime(String filePath) throws FileBasedHelperException { ChannelSftp channelSftp = null; try { channelSftp = getSftpChannel(); int modificationTime = channelSftp.lstat(filePath).getMTime(); return modificationTime; } catch (SftpException e) { throw new FileBasedHelperException( String.format("Failed to get modified timestamp for file at path %s due to error %s", filePath, e.getMessage()), e); } finally { if (channelSftp != null) { channelSftp.disconnect(); } } }
Example 5
Source File: SftpFileSystem.java From commons-vfs with Apache License 2.0 | 6 votes |
/** * Returns a channel to the pool. * * @param channel the used channel. */ protected void putChannel(final ChannelSftp channel) { if (idleChannel == null) { synchronized (this) { if (idleChannel == null) { // put back the channel only if it is still connected if (channel.isConnected() && !channel.isClosed()) { idleChannel = channel; } } else { channel.disconnect(); } } } else { channel.disconnect(); } }
Example 6
Source File: JobEntrySFTPIT.java From pentaho-kettle with Apache License 2.0 | 6 votes |
private void uploadFile( String dir, String file ) throws Exception { Session session = server.createJschSession(); session.connect(); try { ChannelSftp sftp = (ChannelSftp) session.openChannel( "sftp" ); sftp.connect(); try { sftp.mkdir( dir ); sftp.cd( dir ); sftp.put( new ByteArrayInputStream( "data".getBytes() ), file ); } finally { sftp.disconnect(); } } finally { session.disconnect(); } }
Example 7
Source File: SshdSftpEnabledTest.java From sshd-shell-spring-boot with Apache License 2.0 | 5 votes |
@DirtiesContext @Test public void testSftp() throws Exception { Session session = openSession(props.getShell().getUsername(), props.getShell().getPassword()); ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(); sftp.disconnect(); session.disconnect(); assertTrue(new File("target/sftp/admin").exists()); }
Example 8
Source File: SshdSftpEnabledTest.java From sshd-shell-spring-boot with Apache License 2.0 | 5 votes |
@DirtiesContext @Test public void testSftp2() throws Exception { Session session = openSession(props.getShell().getUsername(), props.getShell().getPassword()); ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(); sftp.disconnect(); session.disconnect(); assertTrue(new File("target/sftp/admin").exists()); }
Example 9
Source File: SFtpUInfoTask.java From Aria with Apache License 2.0 | 5 votes |
@Override protected void getFileInfo(Session session) throws JSchException, UnsupportedEncodingException, SftpException { SFtpTaskOption option = (SFtpTaskOption) getWrapper().getTaskOption(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(1000); String remotePath = option.getUrlEntity().remotePath; String temp = CommonUtil.convertSFtpChar(getOption().getCharSet(), remotePath) + "/" + getWrapper().getEntity().getFileName(); SftpATTRS attr = null; try { attr = channel.stat(temp); } catch (Exception e) { ALog.d(TAG, String.format("文件不存在,remotePath:%s", remotePath)); } boolean isComplete = false; UploadEntity entity = getWrapper().getEntity(); if (attr != null && attr.getSize() == entity.getFileSize()) { isComplete = true; } CompleteInfo info = new CompleteInfo(); info.code = isComplete ? ISCOMPLETE : 200; info.obj = attr; channel.disconnect(); callback.onSucceed(getWrapper().getKey(), info); }
Example 10
Source File: SFtpDInfoTask.java From Aria with Apache License 2.0 | 5 votes |
@Override protected void getFileInfo(Session session) throws JSchException, UnsupportedEncodingException, SftpException { SFtpTaskOption option = (SFtpTaskOption) getWrapper().getTaskOption(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(1000); //channel.setFilenameEncoding(option.getCharSet()); //channel.setFilenameEncoding("gbk"); String remotePath = option.getUrlEntity().remotePath; String temp = CommonUtil.convertSFtpChar(option.getCharSet(), remotePath); SftpATTRS attr = null; try { attr = channel.stat(temp); } catch (Exception e) { ALog.e(TAG, String.format("文件不存在,remotePath:%s", remotePath)); } if (attr != null) { getWrapper().getEntity().setFileSize(attr.getSize()); CompleteInfo info = new CompleteInfo(); info.code = 200; callback.onSucceed(getWrapper().getKey(), info); } else { callback.onFail(getWrapper().getEntity(), new AriaSFTPException(String.format("文件不存在,remotePath:%s", remotePath)), false); } channel.disconnect(); }
Example 11
Source File: SftpFsHelper.java From incubator-gobblin with Apache License 2.0 | 5 votes |
@Override public List<String> ls(String path) throws FileBasedHelperException { try { List<String> list = new ArrayList<>(); ChannelSftp channel = getSftpChannel(); Vector<LsEntry> vector = channel.ls(path); for (LsEntry entry : vector) { list.add(entry.getFilename()); } channel.disconnect(); return list; } catch (SftpException e) { throw new FileBasedHelperException("Cannot execute ls command on sftp connection", e); } }
Example 12
Source File: SftpFsHelper.java From incubator-gobblin with Apache License 2.0 | 5 votes |
@Override public long getFileSize(String filePath) throws FileBasedHelperException { try { ChannelSftp channelSftp = getSftpChannel(); long fileSize = channelSftp.lstat(filePath).getSize(); channelSftp.disconnect(); return fileSize; } catch (SftpException e) { throw new FileBasedHelperException( String.format("Failed to get size for file at path %s due to error %s", filePath, e.getMessage()), e); } }
Example 13
Source File: SftpFileObject.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Fetches file attributes from server. * * @throws IOException */ private void statSelf() throws IOException { ChannelSftp channel = getAbstractFileSystem().getChannel(); try { setStat(channel.stat(relPath)); } catch (final SftpException e) { try { // maybe the channel has some problems, so recreate the channel and retry if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) { channel.disconnect(); channel = getAbstractFileSystem().getChannel(); setStat(channel.stat(relPath)); } else { // Really does not exist attrs = null; } } catch (final SftpException innerEx) { // TODO - not strictly true, but jsch 0.1.2 does not give us // enough info in the exception. Should be using: // if ( e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE ) // However, sometimes the exception has the correct id, and // sometimes // it does not. Need to look into why. // Does not exist attrs = null; } } finally { getAbstractFileSystem().putChannel(channel); } }
Example 14
Source File: SftpUtils.java From secure-data-service with Apache License 2.0 | 5 votes |
public static void landFile(File fileToLand, UploadProperties properties) throws Exception { JSch jsch = new JSch(); String host = properties.getSftpServer(); String user = properties.getUser(); String password = properties.getPassword(); int port = properties.getPort(); Session session = jsch.getSession(user, host, port); session.setOutputStream(System.out); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(TIMEOUT); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp c = (ChannelSftp) channel; // delete any existing file with the target filename, so that rename will work @SuppressWarnings("unchecked") Vector<LsEntry> files = c.ls("."); for (LsEntry file : files) { if (FINAL_REMOTE_FILENAME.equals(file.getFilename())) { c.rm(FINAL_REMOTE_FILENAME); } } // transmit file, using temp remote name, so ingestion won't process file until complete c.put(new FileInputStream(fileToLand), TEMP_REMOTE_FILENAME, ChannelSftp.OVERWRITE); // rename remote file so ingestion can begin c.rename(TEMP_REMOTE_FILENAME, FINAL_REMOTE_FILENAME); c.disconnect(); channel.disconnect(); session.disconnect(); }
Example 15
Source File: GitRepo.java From warnings-ng-plugin with MIT License | 4 votes |
/** * Zip bare repository, copy to Docker container using sftp, then unzip. The repo is now accessible over * "ssh://git@ip:port/home/git/gitRepo.git" * * @param host * IP of Docker container * @param port * SSH port of Docker container */ public void transferToDockerContainer(String host, int port) { try { Path zipPath = Files.createTempFile("git", "zip"); File zippedRepo = zipPath.toFile(); String zippedFilename = zipPath.getFileName().toString(); ZipUtil.pack(new File(dir.getPath()), zippedRepo); Properties props = new Properties(); props.put("StrictHostKeyChecking", "no"); JSch jSch = new JSch(); jSch.addIdentity(privateKey.getAbsolutePath()); Session session = jSch.getSession("git", host, port); session.setConfig(props); session.connect(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); channel.cd("/home/git"); channel.put(new FileInputStream(zippedRepo), zippedFilename); ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); InputStream in = channelExec.getInputStream(); channelExec.setCommand("unzip " + zippedFilename + " -d " + REPO_NAME); channelExec.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; int index = 0; while ((line = reader.readLine()) != null) { System.out.println(++index + " : " + line); } channelExec.disconnect(); channel.disconnect(); session.disconnect(); // Files.delete(zipPath); } catch (IOException | JSchException | SftpException e) { throw new AssertionError("Can't transfer git repository to docker container", e); } }