public static ClientSession setupClientSession()

in sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java [120:278]


    public static ClientSession setupClientSession(
            String portOption, BufferedReader stdin, Level level,
            PrintStream stdout, PrintStream stderr, String... args)
            throws Exception {
        int port = -1;
        String host = null;
        String login = null;
        String proxyJump = null;
        String password = null;
        boolean error = false;
        List<Path> identities = new ArrayList<>();
        Map<String, Object> options = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        List<NamedFactory<Cipher>> ciphers = null;
        List<NamedFactory<Mac>> macs = null;
        List<NamedFactory<Compression>> compressions = null;
        int numArgs = GenericUtils.length(args);
        for (int i = 0; (!error) && (i < numArgs); i++) {
            String argName = args[i];
            String argVal = null;
            if (isArgumentedOption(portOption, argName)) {
                i++;
                if (i >= numArgs) {
                    error = CliLogger.showError(stderr, "option requires an argument: " + argName);
                    break;
                }

                argVal = args[i];
            }

            if (portOption.equals(argName)) {
                if (port > 0) {
                    error = CliLogger.showError(stderr, argName + " option value re-specified: " + port);
                    break;
                }

                port = Integer.parseInt(argVal);
                if (port <= 0) {
                    error = CliLogger.showError(stderr, "Bad option value for " + argName + ": " + port);
                    break;
                }
            } else if ("-J".equals(argName)) {
                if (proxyJump != null) {
                    error = CliLogger.showError(stderr, argName + " option value re-specified: " + proxyJump);
                    break;
                }
                proxyJump = argVal;
            } else if ("-w".equals(argName)) {
                if (GenericUtils.length(password) > 0) {
                    error = CliLogger.showError(stderr, argName + " option value re-specified: " + password);
                    break;
                }
                password = argVal;
            } else if ("-c".equals(argName)) {
                ciphers = setupCiphers(argName, argVal, ciphers, stderr);
                if (GenericUtils.isEmpty(ciphers)) {
                    error = true;
                    break;
                }
            } else if ("-m".equals(argName)) {
                macs = setupMacs(argName, argVal, macs, stderr);
                if (GenericUtils.isEmpty(macs)) {
                    error = true;
                    break;
                }
            } else if ("-i".equals(argName)) {
                Path idFile = resolveIdentityFile(argVal);
                identities.add(idFile);
            } else if ("-C".equals(argName)) {
                compressions = setupCompressions(argName, argVal, compressions, stderr);
                if (GenericUtils.isEmpty(compressions)) {
                    error = true;
                    break;
                }
            } else if ("-o".equals(argName)) {
                String opt = argVal;
                int idx = opt.indexOf('=');
                if (idx <= 0) {
                    error = CliLogger.showError(stderr, "bad syntax for option: " + opt);
                    break;
                }

                String optName = opt.substring(0, idx);
                String optValue = opt.substring(idx + 1);
                if (HostConfigEntry.IDENTITY_FILE_CONFIG_PROP.equals(optName)) {
                    Path idFile = resolveIdentityFile(optValue);
                    identities.add(idFile);
                } else {
                    options.merge(optName, optValue, (a, b) -> a + "," + b);
                }
            } else if ("-l".equals(argName)) {
                if (login != null) {
                    error = CliLogger.showError(stderr, argName + " option value re-specified: " + port);
                    break;
                }

                login = argVal;
            } else if (argName.charAt(0) != '-') {
                if (host != null) { // assume part of a command following it
                    break;
                }

                host = argName;
                int pos = host.indexOf('@'); // check if user@host
                if (pos > 0) {
                    if (login == null) {
                        login = host.substring(0, pos);
                        host = host.substring(pos + 1);
                    } else {
                        error = CliLogger.showError(stderr, "Login already specified using -l option (" + login + "): " + host);
                        break;
                    }
                }
            }
        }

        if ((!error) && GenericUtils.isEmpty(host)) {
            error = CliLogger.showError(stderr, "Hostname not specified");
        }

        if (error) {
            return null;
        }

        PropertyResolver resolver = PropertyResolverUtils.toPropertyResolver(options);
        SshClient client = setupClient(
                resolver, ciphers, macs, compressions, identities,
                stdin, stdout, stderr, level, args);
        if (client == null) {
            return null;
        }

        try {
            client.start();

            if (login == null) {
                login = OsUtils.getCurrentUser();
            }

            port = SshConstants.TO_EFFECTIVE_PORT.applyAsInt(port);

            HostConfigEntry entry = resolveHost(client, login, host, port, proxyJump);
            ClientSession session = client.connect(entry, null, null)
                    .verify(CliClientModuleProperties.CONECT_TIMEOUT.getRequired(client))
                    .getSession();
            try {
                if (GenericUtils.length(password) > 0) {
                    session.addPasswordIdentity(password);
                }
                session.auth().verify(CliClientModuleProperties.AUTH_TIMEOUT.getRequired(session));
                return session;
            } catch (Exception e) {
                session.close(true);
                throw e;
            }
        } catch (Exception e) {
            client.close();
            throw e;
        }
    }