Jenkins SSH shell closes before executing remote commands

I have a Jenkins job with the following commands under "Execute shell":

ssh jenkins@172.31.12.58
pwd

I want the Jenkins server to connect via SSH to the remote server then run a command on the remote server.

Instead, Jenkins connects to the remote server, disconnects immediately, then runs the pwd command locally as can be seen in the output:

Started by user Johanan Lieberman
Building in workspace /var/lib/jenkins/jobs/Test Github build/workspace
[workspace] $ /bin/sh -xe /tmp/hudson266272646442487328.sh
+ ssh jenkins@172.31.12.58
Pseudo-terminal will not be allocated because stdin is not a terminal.
+ pwd
/var/lib/jenkins/jobs/Test Github build/workspace
Finished: SUCCESS

Edit : Any idea why the subsequent commands after the ssh command aren't run inside the SSH shell, but rather run locally instead?


If you're not running interactively, SSH does not create an interactive session (thus the "Pseudo-terminal" error message you see), so it's not quite the same as executing a sequence of commands in an interactive terminal.

To run a specific command through an SSH session, use:

ssh jenkins@YOUR_IP 'uname -a'

The remote command must be quoted properly as a single argument to the ssh command. Or use the bash here-doc syntax for a simple multi-line script:

ssh jenkins@YOUR_IP <<EOF
pwd
uname -a
EOF

I think you can use the Publish Over SSH plugin to execute commands on a slave with SSH:

在这里输入图像描述

If the Source files field is mandatory, maybe you can transfer a dummy file.

Update: Another solution is to use the SSH plugin. Maybe it's a better solution compare to the other plugin :)

链接地址: http://www.djcxy.com/p/25320.html

上一篇: 什么是Bash的爆炸美元(!$)?

下一篇: Jenkins SSH shell在执行远程命令之前关闭