I want to run a shell script on remote machine after logging through ssh. Here is my code.
通过ssh登录后,我想在远程机器上运行一个shell脚本。这是我的代码。
#!/bin/bash
USERNAME=user
HOSTS="172.20.16.120"
for HOSTNAME in ${HOSTS} ; do sshpass -p password ssh -t -t ${USERNAME}@${HOSTNAME}
主机名在${主机};sshpass -p密码ssh -t -t ${用户名}@${主机名}
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?
通过ssh登录后,我只需要在ssh作为shell脚本后运行下面的行。我不想将这些行保存在。sh文件中并运行。我需要在jenkins中运行。谁能帮忙吗?
2 个解决方案
#1
1
I suggest you to follow the following steps:
我建议你遵循以下步骤:
- 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:
- 到节点属性>环境变量>,你可以给出配置的名称和值。我的设置如下:
- 将远程机器配置为从属节点。Jenkins提供节点属性到节点属性>环境变量>你可以给出配置的名称和值。我的设置如下:
name: remotemachine1 value: 172.20.16.120
名字:remotemachine1值:172.20.16.120
name:USERNAME value: user
用户名称:用户名:价值
- 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.
- 在您完成Jenkins节点配置之后,您可以创建Jenkins作业并配置Jenkins作业。Jenkins配置构建步骤提供“执行Windows批处理命令”,您可以在那里运行shell脚本。请不要忘记在“限制这个项目可以运行的地方”中指定您的远程机器。
Please let me know if you have any specific further questions.
如果你还有什么具体的问题,请告诉我。
#2
0
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
#1
1
I suggest you to follow the following steps:
我建议你遵循以下步骤:
- 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:
- 到节点属性>环境变量>,你可以给出配置的名称和值。我的设置如下:
- 将远程机器配置为从属节点。Jenkins提供节点属性到节点属性>环境变量>你可以给出配置的名称和值。我的设置如下:
name: remotemachine1 value: 172.20.16.120
名字:remotemachine1值:172.20.16.120
name:USERNAME value: user
用户名称:用户名:价值
- 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.
- 在您完成Jenkins节点配置之后,您可以创建Jenkins作业并配置Jenkins作业。Jenkins配置构建步骤提供“执行Windows批处理命令”,您可以在那里运行shell脚本。请不要忘记在“限制这个项目可以运行的地方”中指定您的远程机器。
Please let me know if you have any specific further questions.
如果你还有什么具体的问题,请告诉我。
#2
0
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