简体   繁体   English

java jsch Suroot(我怎么能?)

[英]java jsch su- root (how can i?)

I want to connect a Linux server and execute a command, but when I send the command sh and su - it requires password, but my program sends password just before it asks. 我想连接Linux服务器并执行命令,但是当我发送命令shsu -它需要密码,但是我的程序在询问之前就发送了密码。 How can i solve this problem? 我怎么解决这个问题?

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.");
        }
    }

}

As you are using a shell channel why do you need to call another sh? 当您使用Shell通道时,为什么需要调用另一个sh?

Any how I do the above is to pass individual command and then read the inputStream until you get back to the prompt and then you can set the next input eg the password or what ever. 我上面的任何操作都是传递单个命令,然后读取inputStream,直到返回提示,然后您可以设置下一个输入,例如密码或任何其他内容。

Pseudo-code being 伪代码被

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM