【问题标题】:java jsch su- root (how can i?)java jsch su-root(我怎么能?)
【发布时间】:2014-05-16 10:17:55
【问题描述】:

我想连接Linux 服务器并执行命令,但是当我发送命令shsu 时 - 它需要密码,但我的程序在它询问之前发送密码。我该如何解决这个问题?

public class Stream
{
    public void getStream(String fileName)
    {
        String user = usernametext.getText();
        String host = hostiptext.getText();
        String password = pass.getText();
        String command4 = "sh\nsu -\nmypassword\n";
        try
        {
            System.out.println("preparing the host information for stream.");
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(10 * 1000);
            Channel channel = session.openChannel("shell");
            InputStream is = new ByteArrayInputStream(command4.getBytes());
            channel.setInputStream(is);
            channel.setOutputStream(System.out);
            channel.connect(15 * 1000);
            session.connect(10 * 10);
            channel.disconnect();
            session.disconnect();
        }
        catch (Exception ex)
        {
            System.out.println("Exception found while streaming.");
        }
    }

}

【问题讨论】:

    标签: java linux jsch


    【解决方案1】:

    当您使用 shell 频道时,为什么需要调用另一个 sh?

    我执行上述操作的方法是传递单个命令,然后读取 inputStream 直到您返回提示,然后您可以设置下一个输入,例如密码什么的。

    伪代码存在

    write "su -"
    readUntil "password:"
    write "******"
    readUntil prompt
    etc.
    

    【讨论】:

    • 我是java新手,不知道怎么做
    • 基本上你是在输入和输出流上读写。见stackoverflow.com/questions/4194439/…
    • 您有解决方案吗,请在此处发布。我正在尝试执行“su”命令,然后在下一行输入 root 密码,我收到类似“标准输入必须是 tty”的错误。我想要的只是使用java在linux中输入root。
    • @thomson 也许这会有所帮助,stackoverflow.com/questions/14220550/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2012-01-29
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    相关资源
    最近更新 更多