【问题标题】:Unable to execute Shell Script in groovy无法在 groovy 中执行 Shell 脚本
【发布时间】:2016-02-01 20:44:51
【问题描述】:

我正在尝试使用以下代码执行 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();

回复:

1 月 28 日星期四 15:00:18 IST 2016:INFO:return code: 1

IST 2016 年 1 月 28 日星期四 15:00:18:错误:标准错误:

1 月 28 日星期四 15:00:18 IST 2016:INFO:standard out:

谢谢 英国熊猫

【问题讨论】:

    标签: shell groovy


    【解决方案1】:

    您正在设置一个频道,然后运行本地命令...

    我相信你需要这样做:

    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 开始)

    【讨论】:

    • 对不起 tim_yates ,它的工作,但在这里我怎样才能捕获脚本的输出。
    • 实际上,我尝试以这种方式捕获“log.info channel.command.toString()”,但它的响应是 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] 这样所以没有得到任何东西。
    • log.info input.text 代替 println input.text 能得到什么?
    • 是的,但是我们想在这里使用这段代码我们如何捕获shall脚本输出?如果你不介意的话,你能告诉我吗
    • input.text 存储在变量中?不太清楚你在问什么
    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    相关资源
    最近更新 更多