5
votes

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

ssh [email protected]
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 [email protected]
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?

2

2 Answers

7
votes

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
1
votes

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

enter image description here

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 :)