Java 在JSch通道上运行脚本,但它没有';t在找不到脚本时引发异常

Java 在JSch通道上运行脚本,但它没有';t在找不到脚本时引发异常,java,groovy,ssh,jsch,ioexception,Java,Groovy,Ssh,Jsch,Ioexception,我正在Groovy中运行一个bash脚本,方法是创建一个到远程主机的JSch隧道,并在一个定义的路径上运行该脚本,该路径带有几个参数。它工作正常,但我希望它在找不到脚本时抛出异常。当我在脚本不存在的情况下手动尝试在远程服务器上运行脚本时,会出现“无此类文件或目录”错误。但是当我通过JSch运行脚本时,我在任何地方都看不到这个错误。其他两个异常,如“Auth failed”或“连接超时”工作正常 import java.io.IOException; import java.io.InputStr

我正在Groovy中运行一个bash脚本,方法是创建一个到远程主机的JSch隧道,并在一个定义的路径上运行该脚本,该路径带有几个参数。它工作正常,但我希望它在找不到脚本时抛出异常。当我在脚本不存在的情况下手动尝试在远程服务器上运行脚本时,会出现“无此类文件或目录”错误。但是当我通过JSch运行脚本时,我在任何地方都看不到这个错误。其他两个异常,如“
Auth failed
”或“
连接超时
”工作正常

import java.io.IOException;
import java.io.InputStream;
import com.jcraft.jsch.*


                    //Connecting to JSch individual sessions
                    JSch jsch = new JSch();
                    jsch.addIdentity( "~/.ssh/id_rsa" );
                    jschSession = jsch.getSession(USERNAME, host, REMOTE_PORT);
                    jschSession.setConfig("StrictHostKeyChecking", "no");
                    jschSession.connect();
                    ChannelExec channelExec = (ChannelExec) jschSession.openChannel("exec");
                    log.info("Channel is open");
                    //Run the shell script with given parameters
                    channelExec.setCommand("sh " + pathtoscript + " \"" + arg1 + "\" "  + arg2);
                    channelExec.setErrStream(System.err);//Not really sure if this works on Groovy?
                    channelExec.connect();

                    InputStream inp = channelExec.getInputStream();
                    byte[] tmp = new byte[1024];
                    String shellOut = "";
                    while (true) {
                            while (inp.available() > 0) {
                                    int i = inp.read(tmp, 0, 1024);
                                    if (i < 0) {
                                            break;
                                    }
                                    shellOut= new String(tmp, 0, i);
                                    log.info(shellOut); //prints out the output when script is run
                            }
                            if (channelExec.isClosed()) {
                                    if (inp.available() > 0) {
                                            continue;
                                    }
                                    log.info("exit-status: " + channelExec.getExitStatus()); //keeps giving me exist-status as 127 when script is not found but I also want it to throw an exception which it doesn't
                                    break;
                            }
                            try {
                                    Thread.sleep(1000);
                                    } catch (Exception ee) {
                                            log.error("This is the error of input");
                            }
                    }

                    channelExec.disconnect();
            }

            catch(JSchException ex) {
                    log.error("Exception cause is: " + ex.getCause());
                    if(ex.getCause().toString().contains("Connection timed out")){
                            log.info("Connection timed out on " + obj.ipAddress);
                            //do something
                   }else(ex.getMessage().contains("Auth fail")){
                            log.info("Authorization failed on " + obj.ipAddress);
                            //do something

                   }
            }
            catch (IOException e) {
                    log.error("Catching exception", e);
                    e.printStackTrace(); //I thought this would print the "Script not found" exception but it doesn't
            }

如果要在命令生成错误输出时引发异常,则必须读取错误输出并将其转换为异常。
channelExec.setErrStream(System.err)
不会为您这样做

有关读取所有命令输出的完整代码,请参阅

在该代码的末尾,在
errorBuffer.toString()
中有错误输出。您将希望抛出一个异常,而不是打印它


您可能还需要检查。非零退出代码通常表示错误。

通常情况下,您没有反馈!你只是不断地发布新问题。
Creating JSch tunnel...
Channel is open
exit-status: 127
Session disconnected.