jenkins 执行ssh 远程linux执行命令

时间:2023-03-08 16:15:52

1、远程机器编写脚本:

脚本名称为: /app/jboss/jboss-as/logs/ALL_SERVICE_STOP.sh

功能为:停止某个服务器某个目录下面的所有应用

#!/bin/bash

path=/app/jboss/jboss-as/logs

for instance in `ls $path|grep ".*.sh"|grep -v ALL_SERVICE_STOP.sh|xargs`;do
cd $path
./$instance stop
done

2、2台linux机器实现远程连接(jenkins服务器也是linux机器)

  a、2台机执行ssh-keygen -t rsa ,生成id_rsa.pub文件

  b、在jenkins linux执行命令:scp /home/xxxx/.ssh/id_rsa.pub xxxx@10.204.52.60:/home/xxxx/.ssh/authorized_keys  输入密码登录

  c、在jenkins linux再执行 scp /home/xxxx/.ssh/id_rsa.pub xxxx@10.204.52.60:/home/xxxx/.ssh/authorized_keys  就不用输入密码登录了

3、jenkins构建执行shell 命令

  ssh -t -p 22 xxxx@xxx.xxx.52.51 " /app/jboss/jboss-as/logs/ALL_SERVICE_STOP.sh"

  就能执行远程命令了。