期待冒险

发布于 2024-11-02 14:17:19 字数 1999 浏览 3 评论 0原文

这是我的脚本

echo "Name:"
read name
if [ "$name" == "abcd" ]; then
    echo "correct username"
    echo "Password:"
    read password
    if [ "$password" == "pwd" ]; then
        echo "Hello"
    else
        echo "Wrong password"
    fi
else
    echo "wrong username"
fi

========================================= ============================================

这是我的Java代码

import java.io.IOException;
import java.util.*;

import expectj.*;

public class Trial {
    public static void main(String[] args) {

        ExpectJ exp = new ExpectJ();
        String command = "sh /root/Desktop/hello.sh";
        Spawn s = null;
        try {           
            s = exp.spawn(command);         
            s.expect("Name:");
            s.send("abcd\n");
            System.out.println("Current status: "+s.getCurrentStandardOutContents());           
            s.expect("correct username");   
            s.expect("Password:");
            s.send("pwd\n");
            s.expect("Hello");
            System.out.println("final output: " + s.getCurrentStandardOutContents());
            System.out.println("Possible errors: " + s.getCurrentStandardErrContents());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("ioe\n");
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("toe\n");
        } finally {
            if (s != null) 
                s.stop();           
        }       
    }
}

=== =================================================== =======================

这是我的输出

Name:
Current status: Name:
correct username
Password:

=================================================== ==========================

它没有进一步进行。它也没有终止..我不知道为什么..

This is my script

echo "Name:"
read name
if [ "$name" == "abcd" ]; then
    echo "correct username"
    echo "Password:"
    read password
    if [ "$password" == "pwd" ]; then
        echo "Hello"
    else
        echo "Wrong password"
    fi
else
    echo "wrong username"
fi

=================================================================================

This is my Java code

import java.io.IOException;
import java.util.*;

import expectj.*;

public class Trial {
    public static void main(String[] args) {

        ExpectJ exp = new ExpectJ();
        String command = "sh /root/Desktop/hello.sh";
        Spawn s = null;
        try {           
            s = exp.spawn(command);         
            s.expect("Name:");
            s.send("abcd\n");
            System.out.println("Current status: "+s.getCurrentStandardOutContents());           
            s.expect("correct username");   
            s.expect("Password:");
            s.send("pwd\n");
            s.expect("Hello");
            System.out.println("final output: " + s.getCurrentStandardOutContents());
            System.out.println("Possible errors: " + s.getCurrentStandardErrContents());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("ioe\n");
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("toe\n");
        } finally {
            if (s != null) 
                s.stop();           
        }       
    }
}

============================================================================

And this is my OUTPUT

Name:
Current status: Name:
correct username
Password:

============================================================================

Its not proceeding further.Its not terminating either.. I dunno why..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

得不到的就毁灭 2024-11-09 14:17:19

当您评论这一行时它是否有效:

System.out.println("Current status: "+s.getCurrentStandardOutContents());

也许应用程序“期望”值“正确的用户名”,但看到“当前状态:名称:”(来自的输出你的“调试”线)。不是 jExpert 方面的专家,但如果该工具只是重定向并监视 System.out,它将看到脚本输出以及打印到控制台的所有内容。

Does it work when you comment this line:

System.out.println("Current status: "+s.getCurrentStandardOutContents());

Maybe the application is "expecting" the value "correct username" but sees "Current status: Name:" instead (the output from your "debug" line). Not an expert on jExpert, but if the tool just redirects and monitors the System.out, it will see the script output as well as everything you print to the console.

圈圈圆圆圈圈 2024-11-09 14:17:19

我明白了..2 个连续的 s.expect() 语句永远无法工作..因此要摆脱它,可以添加 \n ..

在这种情况下

s.expect("正确的用户名");
s.expect("密码:");

肯定不起作用。所以,应该将其替换为-

s.expect("正确的用户名\n密码:");//这将起作用

I get it..2 consecutive s.expect() statements can never work..So to get rid of it, a \n could be added..

In this case

s.expect("correct username");
s.expect("Password:");

is bound not to work.So, it should be replaced by-

s.expect("correct username\nPassword:");//This will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文