gpt4 book ai didi

java jsch su- root (我该怎么办?)

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:22 26 4
gpt4 key购买 nike

我想连接 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 channel 时,为什么需要调用另一个sh?

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

伪代码是

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com