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

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

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

相关问题