Siraj Syed
Siraj Syed

Reputation: 1361

SSH to a server from Jenkins and run shell commands

All,

I am trying to run multiple shell commands on a remote server through Jenkins

I have tried the below code with Execute Shell, plugin sudo su

ssh -o StrictHostKeyChecking=no -i /home/ec2-user/card.pem [email protected] cat /home/ec2-user/testfile.txt 

the problem with this is i can only run one command , for more than 1 I need to run sudo su

ssh -o StrictHostKeyChecking=no -i /home/ec2-user/card.pem [email protected] cat /home/ec2-user/testfile.txt 
ssh -o StrictHostKeyChecking=no -i /home/ec2-user/card.pem [email protected] rm -rf /home/ec2-user/testfile.txt 

how can we achieve running mutiple commands like this?

Upvotes: 2

Views: 8671

Answers (2)

sshepel
sshepel

Reputation: 890

Hey @ Siraj Syed check following example:

String commandToRun = 'cat /home/ec2-user/testfile.txt; rm -rf /home/ec2-user/testfile.txt'

// pipeline step
sh "ssh -o StrictHostKeyChecking=no -i /home/ec2-user/card.pem [email protected] /bin/bash -c '\"${commandToRun}\"'"

Upvotes: 1

Peter McNab
Peter McNab

Reputation: 1101

Can you just do:

ssh -o StrictHostKeyChecking=no -i /home/ec2-user/card.pem [email protected] "cat /home/ec2-user/testfile.txt; rm -rf /home/ec2-user/testfile.txt"

Upvotes: 0

Related Questions