Reputation: 21
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
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
Reputation: 1104
I suggest you to follow the following steps:
name: remotemachine1 value: 172.20.16.120
name:USERNAME value: user
Please let me know if you have any specific further questions.
Upvotes: 3