当前位置: 首页>>代码示例>>Java>>正文


Java ChannelSftp.connect方法代码示例

本文整理汇总了Java中com.jcraft.jsch.ChannelSftp.connect方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelSftp.connect方法的具体用法?Java ChannelSftp.connect怎么用?Java ChannelSftp.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.jcraft.jsch.ChannelSftp的用法示例。


在下文中一共展示了ChannelSftp.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: fileFetch

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public static void fileFetch(String host, String user, String keyLocation, String sourceDir, String destDir) {
    JSch jsch = new JSch();
    Session session = null;
    try {
        // set up session
        session = jsch.getSession(user,host);
        // use private key instead of username/password
        session.setConfig(
                "PreferredAuthentications",
                "publickey,gssapi-with-mic,keyboard-interactive,password");
        jsch.addIdentity(keyLocation);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();

        // copy remote log file to localhost.
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        channelSftp.get(sourceDir, destDir);
        channelSftp.exit();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.disconnect();
    }
}
 
开发者ID:thomas-young-2013,项目名称:wherehowsX,代码行数:29,代码来源:SshUtils.java

示例2: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * 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();
}
 
开发者ID:Azure-Samples,项目名称:acr-java-manage-azure-container-registry,代码行数:32,代码来源:SSHShell.java

示例3: download

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * Perform the specified SSH file download.
 * 
 * @param from source remote file URI ({@code ssh} protocol)
 * @param to target local folder URI ({@code file} protocol)
 */
private static void download(URI from, URI to) {
    File out = new File(new File(to), getName(from.getPath()));
    try (SessionHolder<ChannelSftp> session = new SessionHolder<>(ChannelType.SFTP, from);
            OutputStream os = new FileOutputStream(out);
            BufferedOutputStream bos = new BufferedOutputStream(os)) {

        LOG.info("Downloading {} --> {}", session.getMaskedUri(), to);
        ChannelSftp channel = session.getChannel();
        channel.connect();
        channel.cd(getFullPath(from.getPath()));
        channel.get(getName(from.getPath()), bos);

    } catch (Exception e) {
        throw new RemoteFileDownloadFailedException("Cannot download file", e);
    }
}
 
开发者ID:Nordstrom,项目名称:Remote-Session,代码行数:23,代码来源:SshUtils.java

示例4: download

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * 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();
}
 
开发者ID:Azure-Samples,项目名称:acr-java-manage-azure-container-registry,代码行数:23,代码来源:SSHShell.java

示例5: init

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public SftpClient init() {
    try {
        String host = PropertiesUtil.getString("sftp.host");
        int port = PropertiesUtil.getInt("sftp.port");
        String userName = PropertiesUtil.getString("sftp.user.name");
        String password = PropertiesUtil.getString("sftp.user.password");
        Integer timeout = PropertiesUtil.getInt("sftp.timeout");
        Integer aliveMax = PropertiesUtil.getInt("sftp.aliveMax");
        JSch jsch = new JSch(); // 创建JSch对象
        session = jsch.getSession(userName, host, port); // 根据用户名,主机ip,端口获取一个Session对象
        if (password != null) {
            session.setPassword(password); // 设置密码
        }
        session.setConfig("StrictHostKeyChecking", "no"); // 为Session对象设置properties
        if (timeout != null) session.setTimeout(timeout); // 设置timeout时间
        if (aliveMax != null) session.setServerAliveCountMax(aliveMax);
        session.connect(); // 通过Session建立链接
        channel = (ChannelSftp)session.openChannel("sftp"); // 打开SFTP通道
        channel.connect(); // 建立SFTP通道的连接
        logger.info("SSH Channel connected.");
    } catch (JSchException e) {
        throw new FtpException("", e);
    }
    return this;
}
 
开发者ID:iBase4J,项目名称:iBase4J-Common,代码行数:26,代码来源:SftpClient.java

示例6: openSftpChannel

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * Opens a new SFTP channel over this SSH session.
 *
 * @throws JSchException
 * @throws SftpException
 * @see JSchSftpChannel
 */
public JSchSftpChannel openSftpChannel() throws JSchException, SftpException {
  ChannelSftp chan = (ChannelSftp) session.openChannel("sftp");
  chan.connect(timeout);
  if (url.getPath().isPresent()) {
    String dir = url.getPath().get();
    try {
      chan.cd(dir);
    } catch (SftpException e) {
      logger.warning(e.toString());
      mkdirs(chan, dir);
      chan.cd(dir);
    }
  }
  return new JSchSftpChannel(chan);
}
 
开发者ID:google,项目名称:nomulus,代码行数:23,代码来源:JSchSshSession.java

示例7: ls

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public static List<String> ls(String hostname, int port, String username, File keyFile, final String passphrase,
		String path) throws JSchException, IOException, SftpException {
	Session session = createSession(hostname, port, username, keyFile, passphrase);
	session.connect();
	ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
	channel.connect();
	@SuppressWarnings("unchecked")
	Vector<LsEntry> vector = (Vector<LsEntry>) channel.ls(path);
	channel.disconnect();
	session.disconnect();
	List<String> files = new ArrayList<String>();
	for (LsEntry lse : vector) {
		files.add(lse.getFilename());
	}
	return files;
}
 
开发者ID:ujmp,项目名称:universal-java-matrix-package,代码行数:17,代码来源:JSchUtil.java

示例8: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * Perform the specified SSH file upload.
 * 
 * @param from source local file URI ({@code file} protocol)
 * @param to target remote folder URI ({@code ssh} protocol)
 */
private static void upload(URI from, URI to) {
    try (SessionHolder<ChannelSftp> session = new SessionHolder<>(ChannelType.SFTP, to);
            FileInputStream fis = new FileInputStream(new File(from))) {

        LOG.info("Uploading {} --> {}", from, session.getMaskedUri());
        ChannelSftp channel = session.getChannel();
        channel.connect();
        channel.cd(to.getPath());
        channel.put(fis, getName(from.getPath()));

    } catch (Exception e) {
        throw new RemoteFileUploadFailedException("Cannot upload file", e);
    }
}
 
开发者ID:Nordstrom,项目名称:Remote-Session,代码行数:21,代码来源:SshUtils.java

示例9: connect

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public void connect() {
	if (session != null) {
		throw new IllegalStateException("Already Connected");
	}
	try {
		JSch jSch = new JSch();
		if (knownHosts != null) {
			jSch.setKnownHosts(knownHosts);
		}
		if (identity != null) {
			jSch.addIdentity(identity);
		}
		session = jSch.getSession(user, host, 22);
		Properties properties = new Properties();
		properties.setProperty("StrictHostKeyChecking", "no");
		properties.setProperty("PreferredAuthentications", "publickey");
		session.setConfig(properties);
		session.connect();
		channelSftp = (ChannelSftp) session.openChannel("sftp");
		channelSftp.connect();

		LOGGER.info("connect() - Connected to %s", host);
	} catch (Exception ex) {
		// Failed to connect
		session = null;
		LOGGER.error("connect() - Failed to connect to %s - %s ", host, ex.getMessage());
		throw new RuntimeException(ex);
	}
}
 
开发者ID:gchq,项目名称:stroom-agent,代码行数:30,代码来源:SFTPTransfer.java

示例10: uploadFile

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public static void uploadFile(String hostname, int port, String username, File keyFile, final String passphrase,
		File file, String destinationFolder) throws JSchException, IOException, SftpException {
	Session session = createSession(hostname, port, username, keyFile, passphrase);
	session.connect();
	ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
	channel.connect();
	channel.cd(destinationFolder);
	channel.put(new FileInputStream(file), file.getName());
	channel.disconnect();
	session.disconnect();
}
 
开发者ID:ujmp,项目名称:universal-java-matrix-package,代码行数:12,代码来源:JSchUtil.java

示例11: connectAndGetSftpChannel

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public ChannelSftp connectAndGetSftpChannel() throws JSchException {
    JSch jsch = new JSch();
    jsch.setKnownHosts(context.getFilesDir().getAbsolutePath() + File.separator + "jsch_known_hosts");

    String username = prefs.sshUserName().getOr(null);
    String host = prefs.sshServerAddress().getOr(null);
    String port = prefs.sshServerPort().getOr(null);
    if (username == null || host == null || port == null)
        throw new IllegalStateException("Please enter your server settings first");

    Session session = jsch.getSession(username, host, Integer.parseInt(port));

    UserInfo info = storedOrAskedUserInfo;
    session.setUserInfo(info);
    try {
        session.connect(5000);
        storedOrAskedUserInfo.confirmPasswordRight();
    }
    catch (JSchException je) {
        if ("auth fail".equals(je.getMessage().toLowerCase())) { // undocumented!
            storedOrAskedUserInfo.clearPassword();
        }
        throw je;
    }

    ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
    channel.connect(5000);
    return channel;
}
 
开发者ID:egueli,项目名称:ETStreamHome,代码行数:30,代码来源:SftpManager.java

示例12: _testConnectionSftp

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void _testConnectionSftp(String user, String password, String host, int port, IMoverInterface callbackContext) {
    try {
        JSch jsch = new JSch();

        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();

        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();

        OutputStream output = channel.put(mTestFilename);
        output.write("Hello Alto".getBytes());
        output.close();

        channel.rm(mTestFilename);
        channel.disconnect();

        session.disconnect();

        callbackContext.success();
    } catch (Exception e) {
        callbackContext.error(e.getMessage());
    }
}
 
开发者ID:adamduren,项目名称:cordova-mover,代码行数:27,代码来源:BaseMover.java

示例13: _connectSftp

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void _connectSftp(String user, String password, String host, int port, IMoverInterface callbackContext) throws JSchException {
    JSch jsch = new JSch();

    Session session = jsch.getSession(user, host, port);
    session.setPassword(password);
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect();

    ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
    channel.connect();

    String key = UUID.randomUUID().toString();
    mSftpChannels.put(key, channel);

    callbackContext.success(key);
}
 
开发者ID:adamduren,项目名称:cordova-mover,代码行数:17,代码来源:BaseMover.java

示例14: put

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public void put(Path localPath, String remotePath) throws JSchException, SftpException {
    connectIfNot();
    ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
    try {
        channel.connect();
        logger.info("sftp put, from={}, to={}", localPath, remotePath);
        channel.put(new ByteArrayInputStream(Files.bytes(localPath)), remotePath);
    } finally {
        channel.disconnect();
    }
}
 
开发者ID:neowu,项目名称:cmn-project,代码行数:12,代码来源:SSH.java

示例15: openConnectedChannel

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * Open and Connect a new SFTP channel.
 * 
 * @return a new channel.
 */
protected ChannelSftp openConnectedChannel() throws JSchException {
    Session session = openSession();
    ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
    channel.connect();
    try {
        channel.cd(basePath);
    } catch (SftpException e) {
        throw new IoException(e);
    }
    return channel;
}
 
开发者ID:JumpMind,项目名称:metl,代码行数:17,代码来源:SftpDirectory.java


注:本文中的com.jcraft.jsch.ChannelSftp.connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。