sshpass を使用すると、git clone がクローン中にハングする

sshpass を使用すると、git clone がクローン中にハングする

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 を使用していると仮定します (あなたは私なので、そうしています)。次の戦略に従って問題を解決できます。

  1. キーとパスフレーズを取得する
  2. キーファイルを自動的に使用するようにsshラッパーを設定する
  3. ssh-agent を設定して、ssh からの要求に応じてパスフレーズのプロビジョニングと自動ハンドアウトを有効にします。
  4. ssh-agent にパスフレーズをインストールするには expect を使用します。

@jayhendrenに教えてもらってssh-agent プラグイン

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-Pgit 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

私はこの方法をテストし、スクリプトで広く使用しました。皆さんにも効果があることを願っています。

関連情報