ExpectIt:无法实现sudo -i [英] ExpectIt : Trouble implementing a sudo -i

查看:716
本文介绍了ExpectIt:无法实现sudo -i的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a web shell client and successfully created a simple terminal.

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

I can do basic commands, but I need to do a sudo -i and pass a password.

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

After send the "sudo -i" command, I "expect" the new user (as root) prompt, but the "expect" waits forever. I can see the prompt is as expected.

此代码仅适用于'sudo'操作并且工作正常。常用命令以分开的方法生成。

This code is only for the 'sudo' opperation and works fine. Common commands are made at separated method.

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(如果我在sudo之前尝试这个,一切正常)...

Common commands ( run( String command) method ): This will block at expect.expect() ONLY IF I DO A SUDO ( if I try this BEFORE sudo, all works fine too ) ...

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

错误(期待sudo提示):

The error (expecting the sudo prompt):

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
}

这篇关于ExpectIt:无法实现sudo -i的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆