
有沒有人發現 sshpass 可以為 ssh 或 git 克隆設定短語?
我有一個帶有部署金鑰和密碼的 github 儲存庫
這會導致按預期提示輸入密碼,並在手動鍵入密碼時進行克隆
git clone git@github:me/myrepo.git
這會導致掛起
sshpass -p "secret" -v git clone git@github:me/myrepo.git
發生這種情況似乎是因為搜尋字串永遠不會與實際字串匹配,但似乎無法更改搜尋字串。
SSHPASS searching for password prompt using match "assword"
SSHPASS read: Enter passphrase for key '/home/jenkins/.ssh/id_rsa':
答案1
這是因為您無法使用 sshpass 提供密碼,而只能使用使用者/密碼與私鑰 ssh 中的密碼。
假設您正在使用 Jenkins - 因為您是我,所以您就是我。我們可以按照以下策略解決問題:
- 取得密鑰和密碼
- 設定 ssh 包裝器以自動使用金鑰文件
- 設定 ssh-agent 以根據 ssh 的請求啟用密碼短語和自動分發
- 使用expect在ssh-agent中安裝密碼
感謝@jayhendren 讓我接觸到ssh-代理插件
Jenkins 管道 Groovy 代碼
/**
* generate stand in executable for ssh to ensure we use the correct id and do not look in home's .sshdir
* @return path to shell script wrapper for ssh
*/
def getSshWrapper(def keyPath) {
def wrapper = "${pwd()}/ssh"
writeFile file: wrapper, text: """#!/usr/bin/env sh
/bin/ssh -i ${keyPath} \$*"""
sh "chmod 700 ${wrapper}"
return wrapper
}
/**
* Enable ssh and git to use a deploy key with a passphrase
* @param credentialId jenkins id of private key / passphrase
* @param closure actions to perform
* @return result of actions
*/
def withDeployKey(def credentialId, closure) {
def result
// Start ssh agent and add key
def helperFilesDir = './build/helperFiles'
def envSettings = ["PATH=${helperFilesDir}:${env.PATH}"]
withEnv(envSettings) {
withCredentials([sshUserPrivateKey(credentialsId: credentialId,
passphraseVariable: 'PASSPHRASE',
keyFileVariable: 'KEY_FILE_PATH')]) {
println "Setup Ssh Wrapper to use credentials key"
dir(helperFilesDir) {
getSshWrapper(KEY_FILE_PATH)
}
// Run closure
println "run closure"
sshagent(credentials: [credentialId]) {
result = closure()
}
}
}
return result
}
例子
withDeployKey('my-deploy-key') {
sh "git clone git@github:me/myrepo.git'
}
答案2
您必須passphrase
向sshpass
使用-P
開關發出提示,它會像魅力一樣工作,例如每當我輸入git pull
詢問我的密碼的提示時:
Enter passphrase for key '/home/sinux/.ssh/id_ed25519':
因此我必須sshpass
像下面這樣使用:
sshpass -P "Enter passphrase for key '/home/sinux/.ssh/id_ed25519':" -p <passphrase> git pull
我已經測試了這種方法並在我的腳本中廣泛使用它,希望它也適合您。