bash Java 在运行期望脚本时抛出 Process exitValue: 127
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12113200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java throws Process exitValue: 127 when running expect script
提问by Anthony Byron
I'm trying a new approach to a hitch I've been stuck on. Instead of using expect4j for my SSH connection, (I couldn't figure out a way past blocking consumer runs and issues with closures, see past posts for more info on that if you're knowledgeable and feeling saintly,) I'm going to try to use an expect script. I have a runtime exec coded in to a button.onclick, see below. Why am I getting a 127 exit value? I basically just need this expect script to ssh in, run a single set of expect and send, give me the readout, and that's it...
我正在尝试一种新方法来解决我一直坚持的问题。而不是为我的 SSH 连接使用 expect4j,(我想不出阻止消费者运行和关闭问题的方法,如果你知识渊博并且感觉圣洁,请参阅过去的帖子以获取更多信息,)我要尝试使用期望脚本。我有一个运行时 exec 编码到 button.onclick 中,见下文。为什么我得到 127 退出值?我基本上只需要这个期望脚本来 ssh,运行一组期望和发送,给我读数,就是这样......
I'm using cygwin. Not sure if that's relevant to why this isn't working...is my sh-bang line pointing to the right place? My cygwin install is a full install, all packages, in C:\cygwin.
我正在使用 cygwin。不确定这是否与为什么这不起作用有关……我的 sh-bang 线指向正确的位置吗?我的 cygwin 安装是完整安装,所有包都在 C:\cygwin 中。
Why am I getting a 127 exit value instead of a readout from my server, and how do I alleviate this?
为什么我从我的服务器得到 127 退出值而不是读数,我该如何缓解这种情况?
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( new String [] {"C:\cygwin\bin\bash.exe", "C:\scripts\login.exp"});
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<OUTPUT>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</OUTPUT>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
#!/usr/bin/expect -f
spawn ssh [email protected] password
match_max 100000
expect "/r/nDestination: "
send -- "xxxxxx\r"
expect eof
回答by Jo So
The problem is that you use bash to execute an expect script. You need to use expect to execute an expect script, or bash to execute an expect script by means of a shell commandline (that would be Process proc = rt.exec( new String [] {"C:\\cygwin\\bin\\bash.exe", "-c", "C:\\scripts\\login.exp"});, note the "-c"which I have inserted) which makes use of the magic shebang at the top of your script. Or better, use only the shebang: Process proc = rt.exec( new String [] {"C:\\scripts\\login.exp"});
问题是您使用 bash 来执行期望脚本。您需要使用 expect 来执行期望脚本,或者使用 bash 来通过 shell 命令行(即Process proc = rt.exec( new String [] {"C:\\cygwin\\bin\\bash.exe", "-c", "C:\\scripts\\login.exp"});,请注意"-c"我插入的内容)来执行期望脚本,该命令行使用脚本顶部的魔法 shebang。或者更好的是,只使用shebang:Process proc = rt.exec( new String [] {"C:\\scripts\\login.exp"});
The exit value of 127 is a special exit value, and tells you "command not found". Which makes sense as you expect script contains many words for which no system binaries or shell builtins exist.
127 的退出值是一个特殊的退出值,它告诉您“未找到命令”。这是有道理的,因为您期望脚本包含许多不存在系统二进制文件或 shell 内置程序的单词。

