我正在尝试使用下面的代码执行Shell脚本,但这是尝试在我的本地系统中运行Shell脚本,尽管我已经提供了我的远程Linux服务器的Shell脚本位置。我不知道它为什么这样工作。任何人都可以检查问题所在。
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();
回应:
Thu Jan 28 15:00:18 IST 2016:INFO:return code:1
Thu Jan 28 15:00:18 IST 2016:ERROR:标准错误:
Thu Jan 28 15:00:18 IST 2016:INFO:标准输出:
由于 Pritish Panda
答案 0 :(得分:0)
您正在设置频道,然后运行本地命令......
我相信你需要这样做:
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()
(未经过测试,但我认为应该从looking at this开始)