Search in sources :

Example 46 with Session

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

the class SFtpClientUtils method getRemoteFileList.

@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Vector<LsEntry> lsVec = null;
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        //sftpChannel.lpwd()
        lsVec = (Vector<LsEntry>) sftpChannel.ls(cwd);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
    return lsVec;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 47 with Session

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

the class SFtpClientUtils method getSession.

private static Session getSession(String user, String password, String addr, int port) throws JSchException {
    Session session = jsch.getSession(user, addr, port);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword(password);
    session.connect();
    return session;
}
Also used : Session(com.jcraft.jsch.Session)

Example 48 with Session

use of com.jcraft.jsch.Session in project ignite by apache.

the class StartNodeCallableImpl method call.

/**
 * {@inheritDoc}
 */
@Override
public ClusterStartNodeResult call() {
    JSch ssh = new JSch();
    Session ses = null;
    try {
        if (spec.key() != null)
            ssh.addIdentity(spec.key().getAbsolutePath());
        ses = ssh.getSession(spec.username(), spec.host(), spec.port());
        if (spec.password() != null)
            ses.setPassword(spec.password());
        ses.setConfig("StrictHostKeyChecking", "no");
        ses.connect(timeout);
        boolean win = isWindows(ses);
        info("Windows mode: " + win, spec.logger(), log);
        char separator = win ? '\\' : '/';
        spec.fixPaths(separator);
        String igniteHome = spec.igniteHome();
        if (igniteHome == null)
            igniteHome = win ? DFLT_IGNITE_HOME_WIN : DFLT_IGNITE_HOME_LINUX;
        String script = spec.script();
        if (script == null)
            script = win ? DFLT_SCRIPT_WIN : DFLT_SCRIPT_LINUX;
        String cfg = spec.configuration();
        if (cfg == null)
            cfg = "";
        String id = FILE_NAME_DATE_FORMAT.format(new Date()) + '-' + UUID.randomUUID().toString().substring(0, 8);
        String scriptOutputFileName = id + ".log";
        int spaceIdx = script.indexOf(' ');
        String scriptPath = spaceIdx > -1 ? script.substring(0, spaceIdx) : script;
        String scriptArgs = spaceIdx > -1 ? script.substring(spaceIdx + 1) : "";
        String rmtLogArgs = buildRemoteLogArguments(spec.username(), spec.host());
        String scriptOutputDir;
        String dfltTmpDir = igniteHome + separator + "work" + separator + "log";
        if (win) {
            String tmpDir = env(ses, "%TMPDIR%", dfltTmpDir, WINDOWS_ENCODING);
            if ("%TMPDIR%".equals(tmpDir))
                tmpDir = dfltTmpDir;
            scriptOutputDir = tmpDir + "\\ignite-startNodes";
        } else {
            // Assume Unix.
            String logDir = env(ses, "$TMPDIR", dfltTmpDir);
            scriptOutputDir = logDir + "/ignite-startNodes";
        }
        shell(ses, "mkdir " + scriptOutputDir);
        String scriptOutputPath = scriptOutputDir + separator + scriptOutputFileName;
        String findSuccess;
        if (win) {
            String scriptFileName = scriptOutputDir + '\\' + id + ".bat";
            String createScript = new SB().a("echo \"").a(igniteHome).a('\\').a(scriptPath).a("\" ").a(scriptArgs).a(!cfg.isEmpty() ? " \"" : "").a(cfg).a(!cfg.isEmpty() ? "\"" : "").a(rmtLogArgs).a(" ^> ").a(scriptOutputPath).a(" ^2^>^&^1").a(" > ").a(scriptFileName).toString();
            info("Create script with command: " + createScript, spec.logger(), log);
            shell(ses, createScript);
            try {
                String createTask = new SB().a("schtasks /create /f /sc onstart").a(" /ru ").a(spec.username()).a(" /rp ").a(spec.password()).a(" /tn ").a(id).a(" /np /tr \"").a(scriptFileName).a('\"').toString();
                info("Create task with command: " + createTask, spec.logger(), log);
                shell(ses, createTask);
                String runTask = "schtasks /run /i /tn " + id;
                info("Run task with command: " + runTask, spec.logger(), log);
                shell(ses, runTask);
            } finally {
                String deleteTask = "schtasks /delete /f /tn " + id;
                info("Delete task with command: " + deleteTask, spec.logger(), log);
                shell(ses, deleteTask);
            }
            findSuccess = "find \"" + SUCCESSFUL_START_MSG + "\" " + scriptOutputPath;
        } else {
            // Mac os don't support ~ in double quotes. Trying get home path from remote system.
            if (igniteHome.startsWith("~")) {
                String homeDir = env(ses, "$HOME", "~");
                igniteHome = igniteHome.replaceFirst("~", homeDir);
            }
            String startNodeCmd = new SB().a("nohup ").a("\"").a(igniteHome).a('/').a(scriptPath).a("\"").a(" ").a(scriptArgs).a(!cfg.isEmpty() ? " \"" : "").a(cfg).a(!cfg.isEmpty() ? "\"" : "").a(rmtLogArgs).a(" > ").a(scriptOutputDir).a('/').a(scriptOutputFileName).a(" 2>& 1 &").toString();
            info("Starting remote node with SSH command: " + startNodeCmd, spec.logger(), log);
            shell(ses, startNodeCmd);
            findSuccess = "grep \"" + SUCCESSFUL_START_MSG + "\" " + scriptOutputPath;
        }
        for (int i = 0; i < NODE_START_CHECK_LIMIT; ++i) {
            Thread.sleep(NODE_START_CHECK_PERIOD);
            String res = exec(ses, findSuccess, win ? WINDOWS_ENCODING : null);
            info("Find result: " + res, spec.logger(), log);
            if (res != null && res.contains(SUCCESSFUL_START_MSG))
                return new ClusterStartNodeResultImpl(spec.host(), true, null);
        }
        return new ClusterStartNodeResultImpl(spec.host(), false, "Remote node could not start. " + "See log for details: " + scriptOutputPath);
    } catch (IgniteInterruptedCheckedException e) {
        return new ClusterStartNodeResultImpl(spec.host(), false, e.getMessage());
    } catch (Exception e) {
        return new ClusterStartNodeResultImpl(spec.host(), false, X.getFullStackTrace(e));
    } finally {
        if (ses != null && ses.isConnected())
            ses.disconnect();
    }
}
Also used : IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) ClusterStartNodeResultImpl(org.apache.ignite.internal.cluster.ClusterStartNodeResultImpl) JSch(com.jcraft.jsch.JSch) Date(java.util.Date) InterruptedIOException(java.io.InterruptedIOException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 49 with Session

use of com.jcraft.jsch.Session in project suite by stupidsing.

the class Ssh method execute.

public int execute(String command) throws JSchException, IOException {
    Session session = null;
    ChannelExec channel = null;
    try {
        session = newSession("kenchi.no-ip.org", 22, "sing", "abc123");
        channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(command);
        channel.connect();
        while (!channel.isClosed()) Thread_.sleepQuietly(100);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Copy.stream(channel.getInputStream(), baos);
        baos.close();
        return channel.getExitStatus();
    } finally {
        close(session, channel);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChannelExec(com.jcraft.jsch.ChannelExec) Session(com.jcraft.jsch.Session)

Example 50 with Session

use of com.jcraft.jsch.Session in project scheduling by ow2-proactive.

the class SSHInfrastructureV2 method startNodeImpl.

/**
 * Internal node acquisition method
 * <p>
 * Starts a PA runtime on remote host using SSH, register it manually in the
 * nodesource.
 *
 * @param hostTracker The host on which one the node will be started
 * @param nbNodes number of nodes to deploy
 * @param depNodeURLs list of deploying or lost nodes urls created
 * @throws RMException
 *             acquisition failed
 */
public void startNodeImpl(final HostTracker hostTracker, final int nbNodes, final List<String> depNodeURLs) throws RMException {
    String fs = getTargetOSObj().fs;
    // we set the java security policy file
    ArrayList<String> sb = new ArrayList<>();
    final boolean containsSpace = schedulingPath.contains(" ");
    if (containsSpace) {
        sb.add("-Dproactive.home=\"" + schedulingPath + "\"");
    } else {
        sb.add("-Dproactive.home=" + schedulingPath);
    }
    String securitycmd = CentralPAPropertyRepository.JAVA_SECURITY_POLICY.getCmdLine();
    if (!this.javaOptions.contains(securitycmd)) {
        if (containsSpace) {
            securitycmd += "\"";
        }
        securitycmd += this.schedulingPath + fs + "config" + fs;
        securitycmd += "security.java.policy-client";
        if (containsSpace) {
            securitycmd += "\"";
        }
        sb.add(securitycmd);
    }
    // we set the log4j configuration file
    String log4jcmd = CentralPAPropertyRepository.LOG4J.getCmdLine();
    if (!this.javaOptions.contains(log4jcmd)) {
        // log4j only understands urls
        if (containsSpace) {
            log4jcmd += "\"";
        }
        log4jcmd += "file:";
        if (!this.schedulingPath.startsWith("/")) {
            log4jcmd += "/";
        }
        log4jcmd += this.schedulingPath.replace("\\", "/");
        log4jcmd += "/config/log/node.properties";
        if (containsSpace) {
            log4jcmd += "\"";
        }
        sb.add(log4jcmd);
    }
    // we add extra java/PA configuration
    if (this.javaOptions != null && !this.javaOptions.trim().isEmpty()) {
        sb.add(this.javaOptions.trim());
    }
    CommandLineBuilder clb = super.getDefaultCommandLineBuilder(getTargetOSObj());
    final boolean deployNodesInDetachedMode = PAResourceManagerProperties.RM_NODES_RECOVERY.getValueAsBoolean() || PAResourceManagerProperties.RM_PRESERVE_NODES_ON_SHUTDOWN.getValueAsBoolean();
    if (deployNodesInDetachedMode) {
        // if we do not want to kill the nodes when the RM exits or
        // restarts, then we should launch the nodes in background and
        // ignore the RM termination signal
        clb.setDetached();
    }
    clb.setJavaPath(this.javaPath);
    clb.setRmHome(this.schedulingPath);
    clb.setPaProperties(sb);
    final String nodeName = nodeNameBuilder.generateNodeName(hostTracker);
    clb.setNodeName(nodeName);
    clb.setNumberOfNodes(nbNodes);
    // finally, the credential's value
    String credString;
    try {
        Client currentClient = super.nodeSource.getAdministrator();
        credString = new String(currentClient.getCredentials().getBase64());
    } catch (KeyException e) {
        throw new RMException("Could not get base64 credentials", e);
    }
    clb.setCredentialsValueAndNullOthers(credString);
    // add an expected node. every unexpected node will be discarded
    String cmdLine;
    String obfuscatedCmdLine;
    try {
        cmdLine = clb.buildCommandLine(true);
        obfuscatedCmdLine = clb.buildCommandLine(false);
    } catch (IOException e) {
        throw new RMException("Cannot build the " + RMNodeStarter.class.getSimpleName() + "'s command line.", e);
    }
    // one escape the command to make it runnable through ssh
    if (cmdLine.contains("\"")) {
        cmdLine = cmdLine.replaceAll("\"", "\\\\\"");
    }
    final String finalCmdLine = cmdLine;
    // The final addDeployingNode() method will initiate a timeout that
    // will declare node as lost and set the description of the failure
    // with a simplistic message, since there is no way to override this
    // mechanism we consider only 90% of timeout to set custom description
    // in case of failure and still allow global timeout
    final int shorterTimeout = Math.round((90 * super.nodeTimeOut) / 100);
    JSch jsch = new JSch();
    final String msg = "deploy on " + hostTracker.getResolvedAddress();
    final List<String> createdNodeNames = RMNodeStarter.getWorkersNodeNames(nodeName, nbNodes);
    depNodeURLs.addAll(addMultipleDeployingNodes(createdNodeNames, obfuscatedCmdLine, msg, super.nodeTimeOut));
    addTimeouts(depNodeURLs);
    Session session;
    try {
        // Create ssh session to the hostname
        session = jsch.getSession(this.sshUsername, hostTracker.getResolvedAddress().getHostName(), this.sshPort);
        if (this.sshPassword == null) {
            jsch.addIdentity(this.sshUsername, this.sshPrivateKey, null, null);
        } else {
            session.setPassword(this.sshPassword);
        }
        session.setConfig(this.sshOptions);
        session.connect(shorterTimeout);
    } catch (JSchException e) {
        multipleDeclareDeployingNodeLost(depNodeURLs, "unable to " + msg + "\n" + getStackTraceAsString(e));
        throw new RMException("unable to " + msg, e);
    }
    SSHInfrastructureV2.logger.info("Executing SSH command: '" + finalCmdLine + "'");
    ScheduledExecutorService deployService = Executors.newSingleThreadScheduledExecutor();
    try {
        // Create ssh channel to run the cmd
        ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_OUTPUT_BUFFER_LENGTH);
        ChannelExec channel;
        try {
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(finalCmdLine);
            channel.setOutputStream(baos);
            channel.setErrStream(baos);
            channel.connect();
        } catch (JSchException e) {
            multipleDeclareDeployingNodeLost(depNodeURLs, "unable to " + msg + "\n" + getStackTraceAsString(e));
            throw new RMException("unable to " + msg, e);
        }
        final ChannelExec chan = channel;
        Future<Void> deployResult = deployService.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                while (!shutDown.get() && !checkAllNodesAreAcquiredAndDo(createdNodeNames, null, null)) {
                    if (anyTimedOut(depNodeURLs)) {
                        throw new IllegalStateException("The upper infrastructure has issued a timeout");
                    }
                    // processes live completely independently
                    if (!deployNodesInDetachedMode && chan.getExitStatus() != PROCESS_STILL_RUNNING_VALUE) {
                        throw new IllegalStateException("The jvm process of the node has exited prematurely");
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // we know the cause of this
                        return null;
                    // interruption just exit
                    }
                }
                // Victory
                return null;
            }
        });
        try {
            deployResult.get(shorterTimeout, TimeUnit.MILLISECONDS);
        } catch (ExecutionException e) {
            declareLostAndThrow("Unable to " + msg + " due to " + e.getCause(), depNodeURLs, channel, baos, e);
        } catch (InterruptedException e) {
            deployResult.cancel(true);
            declareLostAndThrow("Unable to " + msg + " due to an interruption", depNodeURLs, channel, baos, e);
        } catch (TimeoutException e) {
            deployResult.cancel(true);
            declareLostAndThrow("Unable to " + msg + " due to timeout", depNodeURLs, channel, baos, e);
        } finally {
            channel.disconnect();
        }
    } finally {
        removeTimeouts(depNodeURLs);
        session.disconnect();
        deployService.shutdownNow();
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ArrayList(java.util.ArrayList) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) CommandLineBuilder(org.ow2.proactive.resourcemanager.utils.CommandLineBuilder) JSch(com.jcraft.jsch.JSch) RMException(org.ow2.proactive.resourcemanager.exception.RMException) Client(org.ow2.proactive.resourcemanager.authentication.Client) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyException(java.security.KeyException) ChannelExec(com.jcraft.jsch.ChannelExec) KeyException(java.security.KeyException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) RMException(org.ow2.proactive.resourcemanager.exception.RMException) ExecutionException(java.util.concurrent.ExecutionException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Aggregations

Session (com.jcraft.jsch.Session)50 JSch (com.jcraft.jsch.JSch)30 JSchException (com.jcraft.jsch.JSchException)22 IOException (java.io.IOException)17 Channel (com.jcraft.jsch.Channel)13 ChannelSftp (com.jcraft.jsch.ChannelSftp)12 ChannelExec (com.jcraft.jsch.ChannelExec)11 File (java.io.File)10 InputStream (java.io.InputStream)8 SftpException (com.jcraft.jsch.SftpException)7 UserInfo (com.jcraft.jsch.UserInfo)7 FileInputStream (java.io.FileInputStream)7 Properties (java.util.Properties)6 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 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 AzureDockerPreferredSettings (com.microsoft.azure.docker.model.AzureDockerPreferredSettings)2 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)2