期待冒险
这是我的脚本
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您评论这一行时它是否有效:
也许应用程序“期望”值
“正确的用户名”
,但看到“当前状态:名称:
”(来自的输出你的“调试”线)。不是 jExpert 方面的专家,但如果该工具只是重定向并监视 System.out,它将看到脚本输出以及打印到控制台的所有内容。Does it work when you comment this line:
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 theSystem.out
, it will see the script output as well as everything you print to the console.我明白了..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