Forum Discussion

Pritish_Panda's avatar
Pritish_Panda
Contributor
9 years ago

How can I capture the output of the ShellScript in Groovy ?

Hi,

  

    I am trying to capture the output of the Shell Script by using below pice of code but it is not working for me .

 

import java.io.InputStream;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.*
import java.io.*
import java.lang.*


JSch jsch = new JSch();
Session session = jsch.getSession("admin","192.168.2.32", 22);
session.setPassword("admin123");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect()
Channel channel = session.openChannel("exec");
channel.connect();
//def command=((ChannelExec)channel).setCommand("bash /home/bedrock/Soapui_Automation/test.sh");
def command = "bash /home/bedrock/Soapui_Automation/test.sh"
def process = command.execute()
def outputStream = new StringBuffer()
def errorStream = new StringBuffer()
process.consumeProcessOutput(outputStream ,errorStream)
process.waitFor()
log.info("return code: ${process.exitValue()}")
log.error("standard error: ${process.err.text}")
log.info("standard out: ${process.in.text}" + outputStream.toString())
channel.disconnect();
session.disconnect();

 

 

I am trying with this code but its not working for me .

 

Responce-: 

Thu Jan 28 15:00:18 IST 2016:INFO:return code: 1

Thu Jan 28 15:00:18 IST 2016:ERROR:standard error:

Thu Jan 28 15:00:18 IST 2016:INFO:standard out:

 

 

Can you check it out where is the problem in the above code ?

 

 

Thanks 

Pritish Panda

   

   

  •  


    import com.jcraft.jsch.*

    Session session = new JSch().getSession("admin","192.168.2.32", 22)
    session.password = "admin123"

    Properties config = [StrictHostKeyChecking:"no"]
    session.config = config
    session.connect()

    Channel channel = session.openChannel("exec")

    channel.inputStream.withReader { input ->
    channel.command = "bash /home/Soapui_Automation/test.sh"
    channel.connect()
    log.info input.text

    }
    channel.disconnect()
    session.disconnect()

     ..

     

     

    Finally I got the code which solved my all problem . By the way Thanks Rao for all your valuable inputs , which really helped me a lot .

     

     

    Thanks 

    Pritish Panda

  •  


    import com.jcraft.jsch.*

    Session session = new JSch().getSession("admin","192.168.2.32", 22)
    session.password = "admin123"

    Properties config = [StrictHostKeyChecking:"no"]
    session.config = config
    session.connect()

    Channel channel = session.openChannel("exec")

    channel.inputStream.withReader { input ->
    channel.command = "bash /home/Soapui_Automation/test.sh"
    channel.connect()
    log.info input.text

    }
    channel.disconnect()
    session.disconnect()

     ..

     

     

    Finally I got the code which solved my all problem . By the way Thanks Rao for all your valuable inputs , which really helped me a lot .

     

     

    Thanks 

    Pritish Panda

  • nmrao's avatar
    nmrao
    Champion Level 3
    You know what? You are running the the shell script on local machine, that is the problem.
    • Pritish_Panda's avatar
      Pritish_Panda
      Contributor

      No, My shell script is in Remote linux server. "/home/bedrock/Soapui_Automation" this is the path where my shell script do exist in my Remote Linux sever .

    • Pritish_Panda's avatar
      Pritish_Panda
      Contributor

      Yes , I am executing the script in my Local machin so how can I move to my remote loction .

       

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        I think this is going out of topic.