Search in sources :

Example 16 with Session

use of com.jcraft.jsch.Session in project gerrit by GerritCodeReview.

the class GitUtil method initSsh.

public static void initSsh(final TestAccount a) {
    final Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    JSch.setConfig(config);
    // register a JschConfigSessionFactory that adds the private key as identity
    // to the JSch instance of JGit so that SSH communication via JGit can
    // succeed
    SshSessionFactory.setInstance(new JschConfigSessionFactory() {

        @Override
        protected void configure(Host hc, Session session) {
            try {
                final JSch jsch = getJSch(hc, FS.DETECTED);
                jsch.addIdentity("KeyPair", a.privateKey(), a.sshKey.getPublicKeyBlob(), null);
            } catch (JSchException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : JSchException(com.jcraft.jsch.JSchException) Host(org.eclipse.jgit.transport.OpenSshConfig.Host) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) Properties(java.util.Properties) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 17 with Session

use of com.jcraft.jsch.Session in project bamboobsc by billchen198318.

the class SFtpClientUtils method get.

/**
	 * 抓遠端檔案然後存到本機 , 多筆
	 * 
	 * @param user
	 * @param password
	 * @param addr
	 * @param port
	 * @param remoteFile
	 * @param localFile
	 * @throws JSchException
	 * @throws SftpException
	 * @throws Exception
	 */
public static void get(String user, String password, String addr, int port, List<String> remoteFile, List<String> localFile) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        for (int i = 0; i < remoteFile.size(); i++) {
            String rf = remoteFile.get(i);
            String lf = localFile.get(i);
            logger.info("get remote file: " + rf + " write to:" + lf);
            sftpChannel.get(rf, lf);
            File f = new File(lf);
            if (!f.exists()) {
                f = null;
                logger.error("get remote file:" + rf + " fail!");
                throw new Exception("get remote file:" + rf + " fail!");
            }
            f = null;
            logger.info("success write:" + lf);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) File(java.io.File) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 18 with Session

use of com.jcraft.jsch.Session in project bamboobsc by billchen198318.

the class SFtpClientUtils method rm.

public static void rm(String user, String password, String addr, int port, List<String> fileName) throws JSchException, SftpException, Exception {
    if (fileName == null || fileName.size() < 1) {
        return;
    }
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        for (String f : fileName) {
            sftpChannel.rm(f);
            logger.warn("success remove file from " + addr + " :" + fileName);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 19 with Session

use of com.jcraft.jsch.Session in project cdap by caskdata.

the class SFTPConnectionPool method connect.

public ChannelSftp connect(String host, int port, String user, String password, String keyFile) throws IOException {
    // get connection from pool
    ConnectionInfo info = new ConnectionInfo(host, port, user);
    ChannelSftp channel = getFromPool(info);
    if (channel != null) {
        if (channel.isConnected()) {
            return channel;
        } else {
            channel = null;
            synchronized (this) {
                --liveConnectionCount;
                con2infoMap.remove(channel);
            }
        }
    }
    // create a new connection and add to pool
    JSch jsch = new JSch();
    Session session = null;
    try {
        if (user == null || user.length() == 0) {
            user = System.getProperty("user.name");
        }
        if (password == null) {
            password = "";
        }
        if (keyFile != null && keyFile.length() > 0) {
            jsch.addIdentity(keyFile);
        }
        if (port <= 0) {
            session = jsch.getSession(user, host);
        } else {
            session = jsch.getSession(user, host, port);
        }
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();
        synchronized (this) {
            con2infoMap.put(channel, info);
            liveConnectionCount++;
        }
        return channel;
    } catch (JSchException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 20 with Session

use of com.jcraft.jsch.Session in project cdap by caskdata.

the class SFTPConnectionPool method disconnect.

void disconnect(ChannelSftp channel) throws IOException {
    if (channel != null) {
        // close connection if too many active connections
        boolean closeConnection = false;
        synchronized (this) {
            if (liveConnectionCount > maxConnection) {
                --liveConnectionCount;
                con2infoMap.remove(channel);
                closeConnection = true;
            }
        }
        if (closeConnection) {
            if (channel.isConnected()) {
                try {
                    Session session = channel.getSession();
                    channel.disconnect();
                    session.disconnect();
                } catch (JSchException e) {
                    throw new IOException(StringUtils.stringifyException(e));
                }
            }
        } else {
            returnToPool(channel);
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) IOException(java.io.IOException) Session(com.jcraft.jsch.Session)

Aggregations

Session (com.jcraft.jsch.Session)41 JSch (com.jcraft.jsch.JSch)23 JSchException (com.jcraft.jsch.JSchException)20 IOException (java.io.IOException)16 Channel (com.jcraft.jsch.Channel)13 ChannelSftp (com.jcraft.jsch.ChannelSftp)11 File (java.io.File)9 ChannelExec (com.jcraft.jsch.ChannelExec)7 SftpException (com.jcraft.jsch.SftpException)7 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)6 UserInfo (com.jcraft.jsch.UserInfo)5 OutputStream (java.io.OutputStream)5 EditableDockerHost (com.microsoft.azure.docker.model.EditableDockerHost)4 DockerHost (com.microsoft.azure.docker.model.DockerHost)3 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)3 AzureDockerPreferredSettings (com.microsoft.azure.docker.model.AzureDockerPreferredSettings)2 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)2 AzureInputDockerLoginCredsDialog (com.microsoft.azuretools.docker.ui.dialogs.AzureInputDockerLoginCredsDialog)2 AzureInputDockerLoginCredsDialog (com.microsoft.intellij.docker.dialogs.AzureInputDockerLoginCredsDialog)2