【问题标题】:How to execute shell script in remote server using Jenkins Active choice parameter groovy script?如何使用 Jenkins Active 选择参数 groovy 脚本在远程服务器中执行 shell 脚本?
【发布时间】:2021-03-21 10:55:51
【问题描述】:

我正在尝试使用 groovy 脚本通过 ssh 在远程服务器中执行 shell 脚本。

注意:源服务器和目标服务器均经过无密码身份验证

Groovy 脚本 test.groovy:

def sout = new StringBuffer(), serr = new StringBuffer()
//def proc = "sh test.sh".execute()
def proc = "sh test.sh".execute()
proc.consumeProcessOutput(sout, serr)
println proc
proc.waitForOrKill(1000)
println sout
println serr
def final_output = sout.tokenize()
println final_output
return final_output

我的 test.sh 包含:

cd /Users/xyz/testing_directory

#show me list of files 
ssh xyz@10.56.45.67 'ls'

现在,当我使用 groovy test.groovy 执行相同操作时,我没有得到任何输出。

但是,如果我在 test.sh 中将“ssh xyz@10.56.45.67 'ls'”行更改为仅“ls”,它会起作用并显示相同源服务器的文件列表。

从 test.sh 中删除 ssh 部分时的工作示例

#show me list of files 
 ls

现在的问题是 groovy 脚本在通过 ssh 连接到远程服务器时无法显示输出

注意:ssh xyz@10.56.45.67 'ls' 在 jenkins 执行 shell 和终端中可以正常工作,但不能在 groovy 脚本中工作

请帮帮我,提前谢谢

【问题讨论】:

  • 如果不为空,请尝试return [seer]。对整个脚本使用try{...} catch (e){ return [e.toString()] }。这将增加您的错误可见性。因为查看错误的唯一方法 - 将其返回到下拉列表。
  • seer 是空的,如果你看到我使用了 print 语句和相同的 test.sh 文件从终端执行,而不是通过 groovy 脚本。
  • println 在主动选择中不起作用。我的意思是return [serr]
  • 我会更新问题部分,因为它看起来很困惑。基本上忘记 Jenkins 部分让我们假设它是正常的 groovy 脚本,我们必须从终端运行它
  • ssh 命令是否可以在没有提示或其他问题的情况下在 shell 中工作?

标签: shell jenkins groovy


【解决方案1】:

最后,我得到了解决方案。

感谢@daggett

def sout = new StringBuffer(), serr = new StringBuffer()
def proc = "ssh username@10.x.x.xx 'ls'".execute()

proc.waitForProcessOutput(sout, serr)
println proc
proc.waitForOrKill(1000)
println sout
println serr
def final_output = sout.tokenize()
println final_output
return final_output

【讨论】:

    猜你喜欢
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多