如何解决Jenkins作业在远程计算机上执行python脚本
要在远程计算机上执行python脚本,为此,我想创建jenkins作业,该作业将输入远程计算机的“ ip”并在该计算机上运行脚本。
解决方法
基本上,只需创建一个脚本并将其放置在OS将从其执行命令的目录中即可。对于linux,其默认路径可能是/usr/bin
。
要执行脚本,
def remoteserverip = "x.x.x.x"
def remotessh = "user@${remoteserverip}"
stage('Example'){
sh "ssh ${remotessh} <your script>"
}
通过设置ssh-keygen,确保您已经允许Jenkins访问远程服务器。
,由于我认为使用crecretials可以提供更大的灵活性,所以我想添加其他选项。
如果要使用具有用户名和密码的ssh ,Jenkins's credetials是一种方法:
node {
withCredentials([[
$class: 'UsernamePasswordMultiBinding',credentialsId: '<credential_id>',usernameVariable: 'USERNAME',passwordVariable: 'PASSWORD']]) {
sh 'sshpass -p "${PASSWORD}" ssh ${USERNAME}@<host> <command>'
}
您还可以在Jenkins中存储私有(和加密的)ssh密钥:
node {
withCredentials([
sshUserPrivateKey(credentialsId: '<credential_id>',keyFileVariable: 'your_keyfile',passphraseVariable: '',usernameVariable: 'USERNAME')]) {
sh 'ssh -i ${your_keyfile} ${USERNAME}@<host> <command>'
}
}
```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。