我想连接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.");
        }
    }

}

最佳答案

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

我上面的任何操作都是传递单个命令,然后读取inputStream,直到返回提示,然后可以设置下一个输入,例如密码或其他。

伪代码被

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

关于java - java jsch Suroot(我怎么能?),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22857072/

10-09 00:15