5

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?

Johannes Liebermann
  • 781
  • 1
  • 8
  • 18
  • Potential duplicate: [What's the Cleanest Way to SSH and Run Multiple Commands in Bash?](http://stackoverflow.com/q/4412238/172599) – Dave Bacher Dec 03 '15 at 19:26

2 Answers2

7

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
Dave Bacher
  • 14,787
  • 2
  • 60
  • 78
1

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

Bruno Lavit
  • 9,486
  • 2
  • 27
  • 37
  • This is a good workaround, however I wanted to know the reason I couldn't accomplish this using the "Execute shell" option. I know it should work that way too. – Johannes Liebermann Dec 03 '15 at 14:33
  • Did you have a look to this plugin? https://wiki.jenkins-ci.org/display/JENKINS/SSH+plugin – Bruno Lavit Dec 03 '15 at 14:39
  • No. My point was to understand why I couldn't open an SSH shell, then run commands inside it the way I described in the original post. I already managed to get it working using your first plugin. – Johannes Liebermann Dec 03 '15 at 14:50
  • OK cool if it works with the first plugin, don't forget to validate my solution ;) – Bruno Lavit Dec 03 '15 at 15:01