English 中文(简体)
com.jcraft.jsch.JSchException: Auth cancel
原标题:
  • 时间:2010-05-25 00:24:24
  •  标签:
  • ant
  • ssh
  • jsch

I m trying to write an Ant script to retrieve an URL via port tunnelling.

It works great when I use a password (the names xxxx d out for privacy):

<project default="main">
  <target name="main">
    <sshsession host="xxxx"
    username="xxxx"
    password="xxxx">
      <LocalTunnel lport="1080" rhost="xxxx" rport="80"/>
      <sequential>
        <get src="http://localhost:1080/xxxx" dest="/tmp/xxxx"/>
      </sequential>
    </sshsession>
  </target>
</project>

But it doesn t work when I use a keyfile, like this:

    <sshsession host="xxxx"
    username="xxxx"
    keyfile="/Users/xxxx/.ssh/id_dsa"
    passphrase="xxxx">
      <LocalTunnel lport="1080" rhost="xxxx" rport="80"/>
      <sequential>
        <get src="http://localhost:1080/xxxx" dest="/tmp/xxxx"/>
      </sequential>
    </sshsession>

I get this exception:

/tmp/build.xml:8: com.jcraft.jsch.JSchException: Auth cancel
    at com.jcraft.jsch.Session.connect(Session.java:451)
    at com.jcraft.jsch.Session.connect(Session.java:150)
    at org.apache.tools.ant.taskdefs.optional.ssh.SSHBase.openSession(SSHBase.java:223)
  • I m sure I m using the correct keyfile (I ve tried using the wrong name, which gives a legitimate FileNotFoundException).
  • I can successfully ssh from the command line without being prompted for a password.
  • I m sure I m using the correct passphrase for the keyfile.

What s the cause of this error and what can I do about it?

最佳回答

I debugged the code. This was failing because my private key was failing authentication; JSch silently fell back to password authentication, which was canceled, because I didn t specify a password.

JSch error handling sucks a lot. Retrace your steps, regenerate a (separate) private key file, use ssh -i to guarantee you re using the right file, and keep your fingers crossed.

问题回答

To get the jsch connection to work, you must specify the paths to both the known_hosts file and to the file containing the private key. This is done using the setKnownHosts and addIdentity methods.

        jsch.setKnownHosts("/path/to/.ssh/known_hosts");
        jsch.addIdentity("/path/to/.ssh/id_rsa");

If the key has a passphrase, you can add it to the addIdentity argument list:

        jsch.addIdentity("/path/to/.ssh/id_rsa", myPassPhrase);

See Javadocs

I had the same issue while using sshexec task. I added passphrase attibute too and it worked fine. create a passphrase for your private key and add this as a attribute in your task. Also don t forget to convert your private key to open ssh format if you generated the key using puttygen on windows.

There is a brand new fork of Jsch out now. The exception handling is far more comprehensive. No more swallowing or defaulting. Head over to https://github.com/vngx/vngx-jsch to check it out. If something doesn t work the way you expect, please raise it as an issue, or send a pull request as we are actively maintaining it. We are also looking to get it up on the maven central repos soon.

I had a similar Issue today. So i thought i will share my solution aswell. I got the same exception but the problem was in fact that i had a umlaut within my password. after choosing a new password without it everything worked fine.

For some reason, jsch use a quite different authentication method.

TLDL; Create a rsa-sha2-512 key (using a PEM format).

ssh-keygen -t rsa-sha2-512 -m PEM -T -f ~/.ssh/id_rsa-sha2-512

In cases where you cantt easily change key type:

  1. recreate a rsa key (using a PEM format).

    ssh-keygen -t rsa -m PEM -T -f ~/.ssh/id_rsa

  2. configure ssh server to accept this method.

    sudo bash -c "echo PubkeyAcceptedAlgorithms +ssh-rsa > /etc/ssh/sshd_config.d/ssh-rsa.conf"

  3. Restart sshd server.

    sudo systemctl restart sshd

  4. now, it works!

PS: JSCH supported types are: ssh-rsa, ssh-dss, ecdca-sha2-nistp256, ecdca-sha2-nistp384, ecdca-sha2-nistp521

https://unix.stackexchange.com/questions/721606/ssh-server-gives-userauth-pubkey-key-type-ssh-rsa-not-in-pubkeyacceptedalgorit





相关问题
ssh issue in a loop

I have a script that connects to a server using ssh. While in a loop, it fails to connect to the second server after connecting to the first one. I guess I have to quit from that server to come back ...

Python SSH paramiko issue - ssh from inside of ssh session

import paramiko client = paramiko.SSHClient() client.load_system_host_keys() ip = 192.168.100.6 client.connect(ip, username= root , password= mima ) i, o, e = client.exec_command( apt-get install ...

Trying to get a terminal to work in Emacs

I ve been having a lot of problems with emacs and trying to get the terminal to work with: M-x term I installed cygwin and I fixed up my .emacs to include the paths: (setenv "PATH" (concat "...

How can I debug a Perl CGI script?

I inherited a legacy Perl script from an old server which is being removed. The script needs to be implemented on a new server. I ve got it on the new server. The script is pretty simple; it ...

ActionScript 3 and SSH

Is there a library for SSH in ActionScript 3? If not, I d appreciate some ideas on how to have Flash integrate with SSH. I have a Flash prototype programmed out, and my client wants to see some ...

Solving thread cleanup on paramiko

I have an automated process using paramiko and have this error: Exception in thread Thread-1 (most likely raised during interpreter shutdown) .... .... <type exceptions.AttributeError >: ...

how to use ping in a script

I want a bash script that ll do: for c in computers: do ping $c if ping is sucessfull: ssh $c check something done If I only do ssh and the computer is iresponsive, it takes forever ...

Changing username in SVN+SSH URI on the fly in working copy

I am using SVN+SSH to check out a working copy of repository from an SVN server on which all developers are members of a developer group and have full read/write permissions on the repository ...

热门标签