问题
we have a unix script which uses expect utility for interactive execution. This script works well when we run from unix server.
If we run this script from Jenkins, it is not working.
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
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
回答1:
Since Jenkins doesn't run it from an actual terminal, interact
doesn't work, since there can't actually be any user interaction there.
You can replace interact
with
expect eof
wait
回答2:
Please use var='xxxxx' instead of var="xxxxx". Hope this help.
回答3:
It seems that Jenkins doesn't support the interactive command model.
Try to use sshpass tool to pass the password in a non-interactive way.
install sshpass
$ sshpass -p "password" {your command}
回答4:
This question is very similar to: Jenkins not waiting on call to a bash script with expect; 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. 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).
回答5:
You may use ssh steps plugin. 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.
Add to your Jenkinsfile :
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"
}
}
来源:https://stackoverflow.com/questions/16914342/expect-utility-is-not-working-when-executing-from-jenkins