简体   繁体   English

如何使用 Jenkins 主动选择参数 groovy 脚本在远程服务器中执行 shell 脚本?

[英]How to execute shell script in remote server using Jenkins Active choice parameter groovy script?

I am trying to execute a shell scripts in a remote server over ssh using groovy scripts.我正在尝试使用 groovy 脚本在 ssh 上的远程服务器中执行 shell 脚本。

Note: Source and Destination servers are passwordless authenticated注意:源服务器和目标服务器经过无密码身份验证

Groovy Scripts test.groovy: Groovy 脚本测试。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

and my test.sh contains:我的 test.sh 包含:

cd /Users/xyz/testing_directory

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

Now when I execute same using groovy test.groovy then I am not getting any output.现在,当我使用 groovy test.groovy 执行相同操作时,我没有得到任何 output。

But if I am changing "ssh xyz@10.56.45.67 'ls'" line to only "ls" in test.sh it works and shows me list of files of same source servers.但是,如果我在 test.sh 中将“ssh xyz@10.56.45.67 'ls'”行更改为仅“ls”,它会起作用并显示相同源服务器的文件列表。

working example when removing ssh part from test.sh从 test.sh 中删除 ssh 部件时的工作示例

#show me list of files 
 ls

Now issue is groovy scripts unable to show outputs while connecting to remote server over ssh现在的问题是 groovy 脚本在通过 ssh 连接到远程服务器时无法显示输出

Note: ssh xyz@10.56.45.67 'ls' works fine in jenkins execute shell as well as terminal only not working in groovy scripts Note: ssh xyz@10.56.45.67 'ls' works fine in jenkins execute shell as well as terminal only not working in groovy scripts

Please help me out and thanks in advance请帮助我,并在此先感谢

Finally after hit and trail I got solution.终于在命中注定之后,我得到了解决方案。

thanks @daggett谢谢@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM