gpt4 book ai didi

java - 期待 : Trouble implementing a sudo -i

转载 作者:行者123 更新时间:2023-11-30 08:03:47 24 4
gpt4 key购买 nike

我正在创建一个 Web shell 客户端并成功创建了一个简单的终端。

enter image description here

我可以执行基本命令,但我需要执行 sudo -i 并传递密码。

enter image description here

发送“sudo -i”命令后,我“期望”新用户(作为 root)提示,但“期望”永远等待。我可以看到提示符合预期。

enter image description here

此代码仅适用于“sudo”操作并且工作正常。通用命令是在单独的方法中执行的。

expect.sendLine( "sudo -i" );
expect.expect( Matchers.contains( ":" ) );
expect.sendLine( password );

// Old prompt was user prompt : user + "@"
// Now I need the root prompt : user + ":"
PROMPT = user + ":";

常用命令(run(String command)方法):仅当我执行 SUDO 时,这才会在 Expect.expect() 处阻塞(如果我在 sudo 之前尝试此操作,一切也都正常)...

expect.sendLine( command );
String result = expect.expect( Matchers.contains( PROMPT ) ).getInput();

错误(需要 sudo 提示):

net.sf.expectit.ExpectIOException: Expect operation fails (timeout: 30000 ms) for matcher: contains('sadlog:')

最佳答案

expect.sendLine( command );
String result = expect.expect( Matchers.contains(":")).getInput();
int pos = result.indexOf(PROMPT);

if (pos > -1) {
// SUCCESS
} else {
// FAILURE
}

关于java - 期待 : Trouble implementing a sudo -i,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31477489/

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