Home > Software engineering >  Why is this ssh keyfile working on Windows/Filezilla and not on linux/command line?
Why is this ssh keyfile working on Windows/Filezilla and not on linux/command line?

Time:03-02

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

CodePudding user response:

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]'
  • Related