java在运行expect脚本时抛出processexitvalue:127

3z6pesqy  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(1037)

我正在尝试一种新的方法来解决我遇到的难题。我将尝试使用expect4j脚本,而不是使用expect4j作为我的ssh连接(我想不出一种方法来阻止使用者运行和闭包问题,如果你有知识并且感觉很圣洁的话,请参阅过去的文章以获得更多信息)。我在一个button.onclick中编写了一个运行时exec,见下文。为什么我得到127出口值?我基本上只需要这个expect脚本来ssh,运行一组expect和send,给我读数,就这样。。。
我在用cygwin。不确定这是否与为什么这不起作用有关…我的shbang线指向正确的地方了吗?我的cygwin安装是一个完整的安装,所有包,在c:\cygwin。
为什么我从服务器得到的是127出口值而不是读数,我该如何减轻这种情况?

  1. try
  2. {
  3. Runtime rt = Runtime.getRuntime();
  4. Process proc = rt.exec( new String [] {"C:\\cygwin\\bin\\bash.exe", "C:\\scripts\\login.exp"});
  5. InputStream stdin = proc.getInputStream();
  6. InputStreamReader isr = new InputStreamReader(stdin);
  7. BufferedReader br = new BufferedReader(isr);
  8. String line = null;
  9. System.out.println("<OUTPUT>");
  10. while ( (line = br.readLine()) != null)
  11. System.out.println(line);
  12. System.out.println("</OUTPUT>");
  13. int exitVal = proc.waitFor();
  14. System.out.println("Process exitValue: " + exitVal);
  15. } catch (Throwable t)
  16. {
  17. t.printStackTrace();
  18. }
  19. # !/usr/bin/expect -f
  20. spawn ssh userid@xxx.xxx.xxx.xxx password
  21. match_max 100000
  22. expect "/r/nDestination: "
  23. send -- "xxxxxx\r"
  24. expect eof
prdp8dxp

prdp8dxp1#

问题是您使用bash来执行expect脚本。您需要使用expect来执行expect脚本,或者使用bash通过shell命令行来执行expect脚本(即 Process proc = rt.exec( new String [] {"C:\\cygwin\\bin\\bash.exe", "-c", "C:\\scripts\\login.exp"}); ,注意 "-c" 我已经插入)它利用了你剧本顶部的魔法。或者更好,只使用shebang: Process proc = rt.exec( new String [] {"C:\\scripts\\login.exp"}); exit值127是一个特殊的exit值,它告诉您“command not found”。这是有意义的,因为您期望脚本包含许多没有系统二进制文件或shell内置的单词。

相关问题