Jenkins pipeline - scp命令使用sshagent获取权限被拒绝

sz81bmfz  于 2023-04-29  发布在  Jenkins
关注(0)|答案(1)|浏览(382)

当使用sshagent Package 器时,我能够将ssh插入服务器A和服务器B,但是当使用scp命令时,我的权限被拒绝

pipeline {
    agent {
        label 'pipeline'
    }
    
    stages {
        stage('SSH to remote host') {

            steps {
                sshagent(credentials: ['myCredentials']) {
                    sh '''
                        ssh userBar@server.a hostname -f
                        ssh userBar@server.a ls -l
                        
                        ssh userFoo@server.b hostname -f
                        ssh userFoo@server.b "echo 'hello world' >> thisFile.txt && ls -l"

                        scp userFoo@server.b:~/thisFile.txt  userBar@server.a:~/thisFile.txt 
                     
                    '''
                }
            }
        }
    }
}

在运行时,ssh命令按预期返回,但scp命令会抛出以下内容

+ scp 'userFoo@server.b:~/thisFile.txt'  'userBar@server.a:~/thisFile.txt'
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
woobm2wo

woobm2wo1#

这是来自scp docs的。看起来scp试图直接从远程主机复制数据。
1.我们需要传递-3选项。

-3      Copies between two remote hosts are transferred through the
         local host.  Without this option the data is copied
         directly between the two remote hosts.  Note that, when
         using the legacy SCP protocol (via the -O flag), this
         option selects batch mode for the second host as scp cannot
         ask for passwords or passphrases for both hosts.  This mode
         is the default.

1.我们可以使用-A选项转发远程代理

-A      Allows forwarding of ssh-agent(1) to the remote system.
         The default is not to forward an authentication agent.

以上两种我都试过了。这两个似乎都在我的设置中工作。

相关问题