0

I am trying to execute a Shell Script by using below code but this is trying run the Shell Script in my local system although I have provided the Shell Script location of my Remote Linux server . I do not have any idea why it is working like that . Can any one check where is the problem .

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 = "bash /home/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();

Response:

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:

Thanks Pritish Panda

Opal
  • 81,889
  • 28
  • 189
  • 210

1 Answers1

0

You're setting up a channel, then running a local command...

I believe you need to do:

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()

    println input.text
}
channel.disconnect()
session.disconnect()

(not tested, but I think that should do it from looking at this)

Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Sorry tim_yates , its working but here how can I capture the output of the Script . – Pritish Panda Feb 01 '16 at 14:07
  • Actually, I tried to capture "log.info channel.command.toString()" in this way but its resonce is Mon Feb 01 19:36:42 IST 2016:INFO:[109, 107, 100, 105, 114, 32, 47, 104, 111, 109, 101, 47, 98, 101, 100, 114, 111, 99, 107, 47, 83, 111, 97, 112, 117, 105, 95, 65, 117, 116, 111, 109, 97, 116, 105, 111, 110, 47, 72, 101, 108, 108, 111] like this so didn't get any thing . – Pritish Panda Feb 01 '16 at 14:08
  • what does `log.info input.text` in place of `println input.text` get you? – tim_yates Feb 01 '16 at 14:31
  • Ya but one think here by using this code how can we capture the shall script output ? Can you tell me if you don't mind then – Pritish Panda Feb 02 '16 at 07:46
  • Store `input.text` in a variable? Not really sure what you're asking – tim_yates Feb 02 '16 at 07:48
  • And It would be a great help if any one can correct the code which I have posted . – Pritish Panda Feb 02 '16 at 07:59
  • I did... I took your code, and made it work... Didn't I? – tim_yates Feb 02 '16 at 08:02
  • Yaa huu , Its working fine .. Thanks man . A Big thanks to you tim_yates . – Pritish Panda Feb 02 '16 at 08:07