Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins SSH shell closes before executing remote commands

Tags:

ssh

jenkins

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?

like image 627
Johannes Liebermann Avatar asked Dec 03 '15 10:12

Johannes Liebermann


2 Answers

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
like image 113
Dave Bacher Avatar answered Sep 22 '22 17:09

Dave Bacher


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

like image 39
Bruno Lavit Avatar answered Sep 21 '22 17:09

Bruno Lavit