ExpectIt:无法实现sudo -i

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

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

此代码仅适用于’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)方法):这将阻止expect.expect()只要我做一个SUDO(如果我在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 } 
Interesting Posts