cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Contributor

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

   

   

1 ACCEPTED SOLUTION

Accepted Solutions
Contributor

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

 


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

View solution in original post

9 REPLIES 9
Community Hero

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

You know what? You are running the the shell script on local machine, that is the problem.


Regards,
Rao.
Contributor

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

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 .

Contributor

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

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

 

 

Community Hero

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

I think this is going out of topic.


Regards,
Rao.
Contributor

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

Please Rao, Don't tell me to open a new thread :'(  . I have been stuck here from last 3 days please give me a solution . 

Community Hero

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

Why the below line commented?

//def command=((ChannelExec)channel).setCommand("bash /home/bedrock/Soapui_Automation/test.sh");


Regards,
Rao.
Contributor

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

Previously I was executing shell script by using the below code only .

 

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");

log.info command

 

And it was working aslo fine for me by using  

 

def command=((ChannelExec)channel).setCommand("bash /home/bedrock/Soapui_Automation/test.sh"); but here I am unable to capture anything it just returns null .Thats why that line is commented .

 

 

Thanks 

Pritish Panda

 

 

 

 

 

 

Community Hero

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

May be you see here to see if that is applicable for you.
http://stackoverflow.com/questions/25789245/how-to-get-jsch-shell-command-output-in-string


Regards,
Rao.
Contributor

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

 


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

View solution in original post

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors