Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过JSch执行shell命令时随机获取空输出_Java_Ssh_Jsch - Fatal编程技术网

Java 通过JSch执行shell命令时随机获取空输出

Java 通过JSch执行shell命令时随机获取空输出,java,ssh,jsch,Java,Ssh,Jsch,我正在尝试使用JSch通过JSch(空间检查命令)执行以下Linux命令: df -h -P |awk '{print $6" "$4}' |awk '{ if ($1 == "/On_Demand") {print}}' | awk '{print $2}' | awk '{ if ($1 ~ /M/) {print ($1*1)} else if($1 ~ /G/) {print ($1*1024)} else if($1 ~ /T/) {print ($1*1024*1024)} els

我正在尝试使用JSch通过JSch(空间检查命令)执行以下Linux命令:

df -h -P |awk '{print $6"  "$4}' |awk '{ if ($1 == "/On_Demand") {print}}' | awk '{print $2}' | awk '{ if ($1 ~ /M/) {print ($1*1)} else if($1 ~ /G/) {print ($1*1024)} else if($1 ~ /T/) {print ($1*1024*1024)} else if($1==0) {print ($1*1)} else if($1 ~ /K/) {print ($1/1024)} else {print ("-1")}}'
备份命令:

export ORACLE_HOME=/path/to/ora/home;export ORACLE_SID=sid;/U01/NEW_DEMO/path/to/ora/home/bin/expdp username/password directory=ON_DEMAND schemas=xyz dumpfile=expdp_xyz.dmp logfile=xyz.log;tail -n 1 /On_Demand/xyz.log
读取输出的代码:

public String getOutput() {
    LOGGER.debug("[getOutput]");
    StringBuffer output = new StringBuffer();
    InputStream in = null;
    if (channel != null && channel.isConnected()) {
        try {
            in = channel.getInputStream();
            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0)
                        break;
                    output.append(new String(tmp, 0, i));
                }
                if (channel.isClosed()) {
                    LOGGER.debug("[getOutput] Channel is closed, so breaking while loop with exit code: "+channel.getExitStatus());
                    break;
                }
            }
        } catch (IOException e) {
            LOGGER.error(e.toString());
            e.printStackTrace();
        } finally {
            channel.disconnect();
        }
    } else {
        System.out.println("Channel is disconnected");
    }

    return output.toString();
}
公共字符串getOutput(){
debug(“[getOutput]”);
StringBuffer输出=新的StringBuffer();
InputStream in=null;
if(channel!=null&&channel.isConnected()){
试一试{
in=channel.getInputStream();
字节[]tmp=新字节[1024];
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);
if(i<0)
打破
append(新字符串(tmp,0,i));
}
if(channel.isClosed()){
debug(“[getOutput]通道已关闭,因此在退出代码为“+Channel.getExitStatus()”的情况下中断while循环);
打破
}
}
}捕获(IOE异常){
LOGGER.error(例如toString());
e、 printStackTrace();
}最后{
通道断开();
}
}否则{
System.out.println(“通道断开”);
}
返回output.toString();
}
问题是,当我尝试读取命令输出时,很多时候我得到了可用空间,但有时我没有得到任何输出。
当再次调用SSH类(比如2次或3次,在建立新会话时)来触发相同的命令时,我得到了输出(这很奇怪)

同样的问题也出现在另一个功能中,我启动一个命令来备份Oracle架构,当我尝试读取Oracle生成的日志文件时(备份和tail命令同时启动,分号分隔),我没有得到任何输出,但日志文件就在那里。多次尝试触发同一个备份命令会给出输出

这是我的工具一个月以来出现的一个大问题

任何有关这方面的帮助都将不胜感激


提前感谢。

我相信您的代码中有一个竞争条件

通道运行它自己的线程来读取数据。线程可以在您在.available()中测试
之后,但在您测试
channel.isConnected()
之前接收数据。虽然like风帽可能很低,但它完美地解释了您所经历的随机行为

请参见如何在
通道.isClosed()
块中再次重新测试.available()
中的

byte[] tmp=new byte[1024];
while(true){
  while(in.available()>0){
    int i=in.read(tmp, 0, 1024);
    if(i<0)break;
    System.out.print(new String(tmp, 0, i));
  }
  if(channel.isClosed()){
    if(in.available()>0) continue; 
    System.out.println("exit-status: "+channel.getExitStatus());
    break;
  }
  try{Thread.sleep(1000);}catch(Exception ee){}
}
byte[]tmp=新字节[1024];
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);
如果(i0)继续;
System.out.println(“退出状态:+channel.getExitStatus());
打破
}
尝试{Thread.sleep(1000);}catch(异常ee){}
}