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" } } } } } }