Pavan Kumar
Pavan Kumar

Reputation: 21

How to run a shell script on remote machine through ssh using jenkins job

I want to run a shell script on remote machine after logging through ssh. Here is my code.

#!/bin/bash 
USERNAME=user
HOSTS="172.20.16.120"

for HOSTNAME in ${HOSTS} ; do sshpass -p password ssh -t -t ${USERNAME}@${HOSTNAME}

echo [QACOHORT-INFO] Space Before clean up

df -h

callworkspace()

{
if [ "$?" = "0" ];

then

for i in `ls`; do 

if [ "$1" = "workspace" ] &&  echo "$i" | grep -q "$VERSION_WS" && [ "$VERSION_WS" != "" ];

then

echo [QACOHORT-INFO] Removing files-in:
pwd

rm -rf $i

echo [QACOHORT-INFO] Removed: $i

fi

if echo "$i" | grep -q "wasabi$VERSION_HUDSON"  && [ "$VERSION_HUDSON" != "" ];

then
echo [QACOHORT-INFO] Removing files-in $i

rm -rf $i/*

elif echo "$i" | grep -q "wasabiSDK$VERSION_HUDSON"  && [ "$VERSION_HUDSON" != "" ];

then

echo [QACOHORT-INFO] Removing files-in $i

#rm -rf $i/*

fi

done

fi
}

unamestr=`uname`

if [ "$unamestr" = "Linux" ];

then

cd /home/jenkin/workspace/Hudson/

callworkspace

cd /home/jenkin/workspace/Hudson/workspace

callworkspace workspace

echo [QACOHORT-INFO] Removing temp files

rm -rf /tmp/tmp* 

rm -rf ~/.local/share/Trash/*

else [ "$unamestr" = "Darwin" ];

cd /Users/ITRU/ws/Hudson/

callworkspace

cd /Users/ITRU/ws/Hudson/workspace

callworkspace workspace

echo [QACOHORT-INFO] Removing temp files

rm -rf /tmp/tmp* 

rm -rf ~/.Trash/*

fi

unamestr=`uname -o`

if [ "$unamestr" = "Cygwin" ];

then

cd D:/work/Hudson

callworkspace

cd D:/work/Hudson/workspace

callworkspace workspace

fi


echo [QACOHORT-INFO] Space after clean up
df -h 
done
exit 0

After logging through ssh, I need to run the below lines after ssh as shell script only. I don't want to keep those lines in .sh file and to run. I need to run it in jenkins. Can anyone help?

Upvotes: 2

Views: 26884

Answers (2)

Yves Schumann
Yves Schumann

Reputation: 44

Stumbled upon an similar problem right now and you could try to solve it this way:

for HOSTNAME in ${HOSTS} ; do 
    sshpass -p password ssh -t -t ${USERNAME}@${HOSTNAME} '(
        pwd
        ls -l
        <put your script here>
    )'
done

Upvotes: 1

Chhabilal
Chhabilal

Reputation: 1104

I suggest you to follow the following steps:

  1. Configure your remote machine as slave node.
    • Jenkins provide Node properties
    • go to Node Properties > Environmental Variables > there you can give the name and value for configuration. I have followings in my setup:

name: remotemachine1 value: 172.20.16.120

name:USERNAME value: user

  1. After you are done with Jenkins Node configuration, you can create a Jenkins Job and configure the Jenkins job.Jenkins configuration Build step provides "Execute Windows batch command" and you can run your shell script there. Please do not forget to specify your remote machine in "Restrict where this project can be run" step inside Jenkins job configuration.

Please let me know if you have any specific further questions.

Upvotes: 3

Related Questions