我希望在通过ssh登录后在远程计算机上运行shell脚本。 这是我的代码。
#!/bin/bash
USERNAME=user
HOSTS="172.20.16.120"
for $ {HOSTS}中的HOSTNAME;做 sshpass -p密码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
通过ssh登录后,我需要在ssh之后运行以下行作为shell脚本。我不想将这些行保存在.sh文件中并运行。我需要在jenkins中运行它。有人可以帮忙吗?
答案 0 :(得分:2)
我建议您按照以下步骤操作:
name:remotemachine1 价值:172.20.16.120
名:USERNAME 值:用户
如果您有任何具体的进一步问题,请与我们联系。
答案 1 :(得分:1)
现在偶然发现了类似的问题,你可以试着这样解决问题:
for HOSTNAME in ${HOSTS} ; do
sshpass -p password ssh -t -t ${USERNAME}@${HOSTNAME} '(
pwd
ls -l
<put your script here>
)'
done