Jenkins在远程机器上执行python脚本的作业



想要在远程机器上执行python脚本,为此我想要创建jenkins作业,该作业将接受输入"ip";并在该计算机上运行脚本。

基本上只需创建一个脚本,并将其放在操作系统执行命令的任何目录中。对于linux,默认路径可能是/usr/bin

为了执行脚本,

def remoteserverip = "x.x.x.x"
def remotessh = "user@${remoteserverip}"
stage('Example'){
sh "ssh ${remotessh} <your script>"
}

通过设置ssh-keygen,确保您已经允许Jenkins访问远程服务器。

由于我认为使用creditials提供了更多的灵活性,所以我想添加其他选项。

如果您想将ssh与用户名和密码一起使用Jenkins的信誉是可行的:

node {
withCredentials([[
$class: 'UsernamePasswordMultiBinding',  credentialsId: '<credential_id>',
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh 'sshpass -p "${PASSWORD}" ssh ${USERNAME}@<host> <command>'
}

您还可以将私人(和加密的(ssh密钥存储在Jenkins:中

node {
withCredentials([
sshUserPrivateKey(credentialsId: '<credential_id>', 
keyFileVariable: 'your_keyfile',
passphraseVariable: '', 
usernameVariable: 'USERNAME')]) {
sh 'ssh -i ${your_keyfile} ${USERNAME}@<host> <command>'
}
}
```

最新更新