首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以root用户身份通过java登录到shell

问以root用户身份通过java登录到shell
EN

Stack Overflow用户
提问于 2020-11-03 05:15:57
回答 1查看 43关注 0票数 0

通过我的java程序,我能够登录到shell并执行cmds。要登录,我提供主机名,用户名,密码,端口。

我的问题是,我只需要通过发出su命令(以root用户身份登录)更改为root用户,然后在密码提示符下提供rootpassword,如下所示。

代码语言:javascript
复制
support@mygateway:/tmp$ su
Password:


BusyBox v1.17.3 (2020-10-08 06:33:26 CEST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

root@mygateway:~#

我可以使用以下方法(已经可用)成功创建SSHShell并执行cmds,而无需根用户访问权限。但是,我如何使用root访问权限登录?

代码语言:javascript
复制
/**
 * Creates the SSHShell
 */
private SSHShellConnectionManager(String host, int port, String userName, String password)
        throws JSchException, IOException {
    Closure expectClosure = getExpectClosure();
    for (String linuxPromptPattern : new String[] { "\\>", "#", "~#", "~\\$",">"}) {
        try {
            Match match = new RegExpMatch(linuxPromptPattern, expectClosure);
            linuxPromptMatches.add(match);
        } catch (MalformedPatternException malformedEx) {
            throw new RuntimeException(malformedEx);
        }
    }
    JSch jsch = new JSch();
    this.session = jsch.getSession(userName, host, port);
    session.setPassword(password);
    Hashtable<String, String> config = new Hashtable<>();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect(60000);
    this.channel = (ChannelShell) session.openChannel("shell");
    //this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
    this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
    channel.connect();
}

/**
 * Executes a command on the remote host.
 *
 * @param command
 *            the command to be executed
 * @param getExitStatus
 *            return the exit status captured in the stdout
 * @param withErr
 *            capture the stderr as part of the output
 * @return the content of the remote output from executing the command
 * @throws Exception
 *             exception thrown
 */
public String executeCommand(String command, Boolean getExitStatus, Boolean withErr) throws Exception {
    String result = "";
    String resultErr = "";

    Channel channel = this.session.openChannel("exec");
    System.out.println("COMMAND TO EXECUTE ON ROUTER " + command);
    ((ChannelExec) channel).setCommand(command);
    InputStream commandOutput = channel.getInputStream();
    InputStream commandErr = ((ChannelExec) channel).getErrStream();
    channel.connect();
    byte[] tmp = new byte[4096];
    while (true) {
        while (commandOutput.available() > 0) {
            int i = commandOutput.read(tmp, 0, 4096);
            if (i < 0) {
                break;
            }
            result += new String(tmp, 0, i);
        }
        while (commandErr.available() > 0) {
            int i = commandErr.read(tmp, 0, 4096);
            if (i < 0) {
                break;
            }
            resultErr += new String(tmp, 0, i);
        }
        if (channel.isClosed()) {
            if (commandOutput.available() > 0) {
                continue;
            }
            if (getExitStatus) {
                result += "exit-status: " + channel.getExitStatus();
                if (withErr) {
                    result += "\n With error:\n" + resultErr;
                }
            }
            break;
        }
        try {
            Thread.sleep(100);
        } catch (Exception ee) {
        }
    }
    channel.disconnect();
    System.out.println("COMMAND RESPONSE :" + result);
    return result;
}



/**
 * Closes shell.
 */
public void close() {
    if (expect != null) {
        expect.close();
    }
    if (channel != null) {
        channel.disconnect();
    }
    if (session != null) {
        session.disconnect();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-03 06:04:52

尝试如下所示:

代码语言:javascript
复制
Channel channel = this.session.openChannel("exec");
((ChannelExec) channel).setCommand("su <<!"); // updated this line
channel.setInputStream(null);
OutputStream out = channel.getOutputStream();
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
((ChannelExec) channel).setPty(true);
channel.connect();
out.write((password + "\n!\n").getBytes()); // updated this line
out.flush();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64653447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档