zoukankan      html  css  js  c++  java
  • Jenkins执行 remote SSH 命令

    1.安装 SSH Pipeline Steps 插件

    2.在凭据中添加remote server凭据,如下

    3.Pipeline编写:

    def GetRemoteServer(ip){
        def remote = [:]
        remote.name = ip
        remote.host = ip
        remote.port = 22
        remote.allowAnyHosts = true
        //通过withCredentials调用Jenkins凭据中已保存的凭据,credentialsId需要填写,其他保持默认即可
        withCredentials([usernamePassword(credentialsId: 'c0c1281-36df-4c5-b26d-dcf5208776f', passwordVariable: 'password', usernameVariable: 'userName')]) {
            remote.user = "${userName}"
            remote.password = "${password}"
        }
        return remote
    }
    
    
    pipeline{
        agent any
        stages{
    
            stage("任务申请"){
                steps{
                     script{
                      //调用上面定义好的方法
                      rserver = GetRemoteServer('10.10.2.11')
                      sshCommand remote: rserver, command: "ifconfig" //在remote server上执行ifconfig命令
    
                     }
                    }
                }
            }
    
        }
    }

    方法二,不定义方法,直接使用:

    def remote = [:]
    remote.name = 'saltm'
    remote.host = '10.10.2.11'
    remote.user = 'root'
    remote.password = '1qjRg'
    remote.allowAnyHosts = true
    
    pipeline{
        agent any
        stages{
    
            stage("任务申请"){
                steps{
                     script{
                         
                      sshCommand remote: remote, command: "ifconfig |grep inet"
                     }
                    }
                }
            }
            
        }
    }
  • 相关阅读:
    更改ORACLE_HOME_NAME
    Linux 查看裸设备大小
    Linux 日常操作
    Linux 高 wio 分析
    Linux 大页: HugePage 与transparent HugePage
    CentOS 8 配置本地YUM源
    MySQL 表压缩
    HIVE 处理json结构数据
    Emacs org-mode 2 文档结构
    CentOS 7 Oracle 19.3 单实例静默安装
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/13524138.html
Copyright © 2011-2022 走看看