为什么这个 ssh 密钥文件在 Windows/Filezilla 上工作,而不在 linux/命令行上工作?

发布于 2025-01-09 22:48:17 字数 3329 浏览 0 评论 0原文

我使用名为 mykey.ppk 的密钥成功连接到 Windows 上的 FileZilla,

我正在尝试使用该密钥在 Linux 上的 Jenkins 管道中上传文件。

我根本无法让该文件在 ubuntu 中工作 20:04

我使用 PuttyGen 将文件转换为名为 mykey_open.ppk 的 open-ssh 格式文件,如 https://serverfault.com/questions/1004774/load-key-privkey-ppk-invalid- format (加载 > 转换菜单 > 导出 OpenSSH 文件)

我将文件的权限设置为 600,所有者为 jenkins:jenkins

我输入了以下命令在 putty 上,

ssh -Tv [电子邮件受保护] -i ./mykey_open.ppk

结果:

debug1: Trying private key: ./mykey_open.ppk
Load key "./mykey_open.ppk": Permission denied
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
[email protected] : Permission denied (publickey).

以及 Jenkins 管道中:

sh 'ssh -Tv [email protected] -i ./mykey_open.ppk'

给出:

Transferred: sent 2520, received 2244 bytes, in 0.4 seconds
Bytes per second: sent 7154.6, received 6371.0
debug1: Exit status 1
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

我还尝试使用 pipeline 命令

def remote = [:]
remote.name = "myremote"
remote.host = "myremote.site.io"
remote.allowAnyHosts = true
    withCredentials([sshUserPrivateKey(keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'myuser')]) {
        remote.user = userName
        remote.identityFile = "mykey_open.ppk"
        stage("SSH Steps Rocks!") {
            sshPut remote: remote, from: 'myfile.zip', into: '/myremote.site.io/path/to/folder'
        }

给出

java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:221)
    at com.cloudbees.plugins.credentials.CredentialsProvider.findCredentialById(CredentialsProvider.java:877)
    at com.cloudbees.plugins.credentials.CredentialsProvider.findCredentialById(CredentialsProvider.java:855)
    at org.jenkinsci.plugins.credentialsbinding.MultiBinding.getCredentials(MultiBinding.java:195)
    at org.jenkinsci.plugins.credentialsbinding.impl.SSHUserPrivateKeyBinding.bind(SSHUserPrivateKeyBinding.java:94)
    at org.jenkinsci.plugins.credentialsbinding.impl.BindingStep$Execution2.doStart(BindingStep.java:134)
    at org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution.lambda$run$0(GeneralNonBlockingStepExecution.java:77)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Finished: FAILURE

I successfully connect to FileZilla on Windows with a key named mykey.ppk

I am trying to use that key to upload a file in a Jenkins pipeline on Linux.

I can't get the file to work at all in ubuntu 20:04

I converted the file to an open-ssh format file named mykey_open.ppk using PuttyGen as indicated in https://serverfault.com/questions/1004774/load-key-privkey-ppk-invalid-format (Load > Conversions menu > Export OpenSSH file)

I set the permissions of the file to 600 with owner jenkins:jenkins

I entered the following command on putty,

ssh -Tv [email protected] -i ./mykey_open.ppk

result:

debug1: Trying private key: ./mykey_open.ppk
Load key "./mykey_open.ppk": Permission denied
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
[email protected] : Permission denied (publickey).

and also in the Jenkins pipeline:

sh 'ssh -Tv [email protected] -i ./mykey_open.ppk'

which gives:

Transferred: sent 2520, received 2244 bytes, in 0.4 seconds
Bytes per second: sent 7154.6, received 6371.0
debug1: Exit status 1
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

I also tried using the pipeline command

def remote = [:]
remote.name = "myremote"
remote.host = "myremote.site.io"
remote.allowAnyHosts = true
    withCredentials([sshUserPrivateKey(keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'myuser')]) {
        remote.user = userName
        remote.identityFile = "mykey_open.ppk"
        stage("SSH Steps Rocks!") {
            sshPut remote: remote, from: 'myfile.zip', into: '/myremote.site.io/path/to/folder'
        }

which gives

java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:221)
    at com.cloudbees.plugins.credentials.CredentialsProvider.findCredentialById(CredentialsProvider.java:877)
    at com.cloudbees.plugins.credentials.CredentialsProvider.findCredentialById(CredentialsProvider.java:855)
    at org.jenkinsci.plugins.credentialsbinding.MultiBinding.getCredentials(MultiBinding.java:195)
    at org.jenkinsci.plugins.credentialsbinding.impl.SSHUserPrivateKeyBinding.bind(SSHUserPrivateKeyBinding.java:94)
    at org.jenkinsci.plugins.credentialsbinding.impl.BindingStep$Execution2.doStart(BindingStep.java:134)
    at org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution.lambda$run$0(GeneralNonBlockingStepExecution.java:77)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Finished: FAILURE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2025-01-16 22:48:17

找到解决方案:使用sftp命令而不是ssh

但 sftp 通常依赖于交互式命令:您输入 sftp,然后您会收到 sftp> 提示,输入诸如 put之类的命令get 等。

在编程上下文中使用该命令有一个解决方法:

echo put localfile.txt path/to/local/remotefile.txt | sftp -i keyfile.ppk [email protected]

在 Jenkins 管道的特定情况下,这将变为:

sh 'echo put localfile.txt path/to/local/remotefile.txt | sftp -i keyfile.ppk [email protected]'

Found the solution: use sftp command instead of ssh.

But sftp usually relies on interactive commands: you type sftp, then you get an sftp> prompt to enter commands such as put, get, etc.

There is a workaround for using the command in a programming context:

echo put localfile.txt path/to/local/remotefile.txt | sftp -i keyfile.ppk [email protected]

In the specific case of a Jenkins pipeline, this becomes:

sh 'echo put localfile.txt path/to/local/remotefile.txt | sftp -i keyfile.ppk [email protected]'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文