0

I am able to execute Shell script command Soap UI and not sure how to read and store the echo output to a variable.

test1.sh contains below commands

CURRENTDATEONLY=`date +"%b %d, %Y"`
echo Current Date is: ${CURRENTDATEONLY}
sleep 10

I am able to execute Shell script command Soap UI and not sure how to read and store the echo output to a variable.

I have tried below code, still not capturing the output.
def command = "C:/Program Files (x86)/PuTTY/putty.exe -ssh user@ip -pw pass  -m C:/test1.sh"

def Process p = Runtime.getRuntime().exec(command);
p.waitFor()

BufferedReader is1 =   new BufferedReader(new InputStreamReader(p.getInputStream())); 
log.info is1
        String line; 

        // reading the output 
        while ((line = is1.readLine()) != null) 

     log.info line ;
====================================================
Tried using jsch api as well, but no luck

import com.jcraft.jsch.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

        String user = "";
        String pw = "";
        String host = "";
        int port = 22;
        String remoteFile = "C:/test1.sh";

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setPassword(pw);
            //session.setConfig("StrictHostKeyChecking", "no");
            log.info("Establishing Connection...");
            session.setTimeout(60000);
            session.connect();
            log.info("Connection established.");
            log.info("Crating SFTP Channel.");
            ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
            sftpChannel.connect();
            log.info("SFTP Channel created.");

            InputStream inputStream = sftpChannel.get(remoteFile);

            Scanner scanner = new Scanner(new InputStreamReader(inputStream)) 
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    log.info(line);
                }

        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        }

Expectation is to read and store CURRENTDATEONLY variable to properties

  • there are a lot of questions `read process output`. example: https://stackoverflow.com/questions/159148/groovy-executing-shell-commands – daggett Jun 27 '19 at 07:36
  • i have tried using getInputStream from Process class and still not able to capture the output. – user2064686 Jul 01 '19 at 09:38
  • could you run the putty from windows console (cmd)? do you see output in windows console? – daggett Jul 01 '19 at 10:21
  • Yes, able to run from cmd and getting output in console – user2064686 Jul 01 '19 at 11:59
  • i mentioned `windows console` and not a putty console. i believe putty starts it's own console and windows console can't read it. – daggett Jul 01 '19 at 12:12
  • Yes you are right, i tried through cmd by using below entry and everything printed on putty console. C:\Program Files (x86)\PuTTY>putty.exe -ssh user@ip -pw passeord -m C:/test1.sh – user2064686 Jul 02 '19 at 04:00

0 Answers0