简体   繁体   English

期望从jenkins执行时,实用程序不起作用

[英]expect utility is not working when executing from jenkins

we have a unix script which uses expect utility for interactive execution. 我们有一个unix脚本,它使用expect实用程序进行交互式执行。 This script works well when we run from unix server. 当我们从unix服务器运行时,此脚本运行良好。

If we run this script from Jenkins, it is not working. 如果我们从Jenkins运行这个脚本,它就无法运行。

Below is the script 下面是脚本

var="xxxxx"
expect -c "
    spawn sudo cp /abcd/sjws/config/obj.conf /abcd/sjws/config/obj.conf_jenkins
    expect {
    "Password:" { send $var\r;interact }
    }
    exit
    "

Below is the output when we run from jenkins 以下是我们从jenkins运行时的输出

spawn sudo cp /abcd/sjws/config/obj.conf /abcd/sjws/config/obj.conf_jenkins 
Password: 
Password:
Build step 'Execute shell' marked build as failure 
Finished: FAILURE

Since Jenkins doesn't run it from an actual terminal, interact doesn't work, since there can't actually be any user interaction there. 由于Jenkins不从实际终端运行它,因此interact不起作用,因为那里实际上不存在任何用户交互。

You can replace interact with 您可以替换interact

expect eof
wait

Please use var='xxxxx' instead of var="xxxxx". 请使用var ='xxxxx'而不是var =“xxxxx”。 Hope this help. 希望这有帮助。

It seems that Jenkins doesn't support the interactive command model. 似乎Jenkins不支持交互式命令模型。

Try to use sshpass tool to pass the password in a non-interactive way. 尝试使用sshpass工具以非交互方式传递密码。

install sshpass 安装sshpass

$ sshpass -p "password" {your command}

This question is very similar to: Jenkins not waiting on call to a bash script with expect ; 这个问题非常类似于: Jenkins没有等待调用bash脚本的期望 ; for those encountering the similar problem, especially when using " su " utility to change the user, please be aware that Jenkins closes the SSH connection as soon as the user is changed. 对于那些遇到类似问题的人, 特别是在使用“ su ”实用程序来更改用户时,请注意一旦用户更改,Jenkins就会关闭SSH连接。 You can not continue giving commands with the new user. 您无法继续向新用户发出命令。 Design your script in such a way that you are sending the command directly, such as: 以直接发送命令的方式设计脚本,例如:

spawn su - username -c "yoursinglewordcommand"

and then the rest of the expect script as usual (I was also successful with the interact command). 然后像往常那样使用expect脚本的其余部分(我也使用了interact命令成功)。

You may use ssh steps plugin . 您可以使用ssh steps插件 This plugin will provide steps like sshScript which executes given script(file) on remote node and responds with output, sshCommand that executes given command on remote node and responds with output and also sshPut, sshGet, sshRemove. 这个插件将提供类似sshScript的步骤,它在远程节点上执行给定的脚本(文件)并响应输出,sshCommand在远程节点上执行给定的命令并响应输出以及sshPut,sshGet,sshRemove。

Add to your Jenkinsfile : 添加到您的Jenkins文件:

node {
   def remote = [:]
   remote.name = 'test'
   remote.host = 'test.domain.com'
   remote.user = 'root'
   remote.password = 'password'
   remote.allowAnyHosts = true
   stage('Remote SSH') {
      sshCommand remote: remote, command: "cp /abcd/sjws/config/obj.conf /abcd/sjws/config/obj.conf_jenkins"
  }
}

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

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