public String runCommand()

in src/main/java/co/elastic/support/util/RemoteSystem.java [101:152]


    public String runCommand(String cmd) {
        if(disabledForPlatform){
            logger.info(Constants.CONSOLE, "Windows is not supported for remote calls at this time");
            return "No Content available: Incompatible platform.";
        }

        // If it's sudo we need to pipe the password into the input stream
        // Only there if if's been set, otherwise just prepending an empty string
        cmd = sudo + cmd;

        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final ByteArrayOutputStream errout = new ByteArrayOutputStream();
        StringBuffer sb = new StringBuffer();

        InputStream istream = null;
        ChannelExec channel = null;
        try {
            session.setTimeout(0);
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(cmd);
            channel.setInputStream(null);
            channel.setPty(true);
            istream = channel.getInputStream();
            channel.connect();
            byte[] tmp=new byte[1024];
            while(true){
                while(istream.available()>0){
                    int i=istream.read(tmp, 0, 1024);
                    if(i<0)break;
                    sb.append(new String(tmp, 0, i));
                }
                if(channel.isClosed()){
                    if(istream.available()>0) continue;
                    logger.error( "Cmd: {}, Exit status: {}", scrubCommandText(cmd), channel.getExitStatus());
                    break;
                }
                try{
                    TimeUnit.SECONDS.sleep(1);}catch(Exception ee){}
            }
        }
        catch(Exception e){
            logger.info(Constants.CONSOLE, "Failed remote command: {}. {}", cmd, Constants.CHECK_LOG) ;
            logger.error("System command failed.", e);
        }
        finally {
            if (channel!= null && channel.isConnected()) {
                channel.disconnect();
            }
        }

        return sb.toString();
    }