Java Code Examples for com.jcraft.jsch.session#setServerAliveInterval()

The following examples show how to use com.jcraft.jsch.session#setServerAliveInterval() . 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: TelemetryTools.java    From anx with Apache License 2.0 5 votes vote down vote up
List<String> execSSHCommand(String command) throws JSchException, IOException {
    JSch jSch = new JSch();

    Window loadingWindow = showLoadingWindow("SSH exec: " + command);

    Session session = jSch.getSession(view.username, view.host, 22);
    session.setDaemonThread(true);
    session.setTimeout(3600000);
    session.setServerAliveInterval(15000);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword(view.password);
    try {
        session.connect();

        Channel channel = session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
        channel.setInputStream(null);
        InputStream input = channel.getInputStream();
        channel.connect();
        List<String> result = new BufferedReader(new InputStreamReader(input)).lines().collect(Collectors.toList());

        channel.disconnect();
        session.disconnect();
        return result;
    } finally {
        loadingWindow.close();
    }
}
 
Example 2
Source File: SftpClient.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Session createSftpSession(JSch jsch, boolean withUserInfo) throws JSchException {
    LOGGER.fine("Creating new SFTP session...");
    String host = configuration.getHost();
    int port = configuration.getPort();
    int timeout = configuration.getTimeout() * 1000;
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "Will connect to {0} [timeout: {1} ms]", new Object[] {host, timeout});
    }
    int keepAliveInterval = configuration.getKeepAliveInterval() * 1000;
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "Keep-alive interval is {0} ms", keepAliveInterval);
    }
    String username = configuration.getUserName();
    String password = configuration.getPassword();
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "Login as {0}", username);
    }
    Session session = jsch.getSession(username, host, port);
    if (StringUtils.hasText(password)) {
        session.setPassword(password);
    }
    // proxy
    setProxy(session, host);
    if (withUserInfo) {
        LOGGER.fine("Setting user info...");
        session.setUserInfo(new SftpUserInfo(configuration));
    }
    session.setTimeout(timeout);
    // keep-alive
    if (keepAliveInterval > 0) {
        session.setServerAliveInterval(keepAliveInterval);
    }
    return session;
}
 
Example 3
Source File: JSchChannelsSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Session startNewSession(boolean acquireChannel) throws JSchException, InterruptedException {
    Session newSession = null;
    final AtomicBoolean cancelled = new AtomicBoolean(false);

    ConnectingProgressHandle.startHandle(env, new Cancellable() {
        @Override
        public boolean cancel() {
            cancelled.set(true);
            return true;
        }
    });

    try {
        while (!cancelled.get()) {
            try {
                newSession = jsch.getSession(env.getUser(), env.getHostAddress(), env.getSSHPort());
                int serverAliveInterval = Integer.getInteger("jsch.server.alive.interval", 0); // NOI18N
                if (serverAliveInterval > 0) {
                    newSession.setServerAliveInterval(serverAliveInterval);
                    int serverAliveCount = Integer.getInteger("jsch.server.alive.count", 5); // NOI18N
                    newSession.setServerAliveCountMax(serverAliveCount);
                }
                newSession.setUserInfo(userInfo);

                for (Entry<String, String> entry : jschSessionConfig.entrySet()) {
                    newSession.setConfig(entry.getKey(), entry.getValue());
                }
                Authentication auth = Authentication.getFor(env);
                final String preferredAuthKey = "PreferredAuthentications"; // NOI18N
                if (!jschSessionConfig.containsKey(preferredAuthKey)) {
                    String methods = auth.getAuthenticationMethods().toJschString();
                    if (methods != null) {
                        log.finest("Setting auth method list to " + methods); //NOI18N
                        newSession.setConfig(preferredAuthKey, methods);
                    }
                }
                if (USE_JZLIB) {
                    newSession.setConfig("compression.s2c", "[email protected],zlib,none"); // NOI18N
                    newSession.setConfig("compression.c2s", "[email protected],zlib,none"); // NOI18N
                    newSession.setConfig("compression_level", "9"); // NOI18N
                }

                if (RemoteStatistics.COLLECT_STATISTICS && RemoteStatistics.COLLECT_TRAFFIC) {
                    newSession.setSocketFactory(MeasurableSocketFactory.getInstance());
                }

                newSession.connect(auth.getTimeout()*1000);
                break;
            } catch (JSchException ex) {
                if (!UNIT_TEST_MODE) {
                    String msg = ex.getMessage();
                    if (msg == null) {
                        throw ex;
                    }
                    if (msg.startsWith("Auth fail") || msg.startsWith("SSH_MSG_DISCONNECT: 2")) { // NOI18N
                        PasswordManager.getInstance().clearPassword(env);
                    }
                } else {
                    throw ex;
                }
            } catch (CancellationException cex) {
                cancelled.set(true);
            }
        }

        if (cancelled.get()) {
            throw new InterruptedException("StartNewSession was cancelled ..."); // NOI18N
        }

        // In case of any port-forwarding previously set for this env
        // init the new session appropriately
        portForwarding.initSession(newSession);

        sessions.put(newSession, new AtomicInteger(JSCH_CHANNELS_PER_SESSION - (acquireChannel ? 1 : 0)));

        log.log(Level.FINE, "New session [{0}] started.", new Object[]{System.identityHashCode(newSession)}); // NOI18N
    } finally {
        ConnectingProgressHandle.stopHandle(env);
    }
    return newSession;
}