1

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

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

此代码仅适用于“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 + ":";

常用命令(运行(字符串命令)方法):仅当我执行 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:')
4

1 回答 1

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

if (pos > -1) {
    // SUCCESS
} else {
    // FAILURE
}
于 2017-01-27T06:31:42.393 回答