带有工作密码的“com.jcraft.jsch.JSchException:Auth fail”

时间:2013-06-17 13:39:07

标签: java jsch

在尝试将文件上传到我们的服务器时,我收到以下异常

    com.jcraft.jsch.JSchException: Auth fail
        at com.jcraft.jsch.Session.connect(Session.java:464)
        at com.jcraft.jsch.Session.connect(Session.java:158)
        at FtpService.transferFileToReciever(FtpService.java:80)
        at FtpService.transferFileToReciever(FtpService.java:54)
        at FtpService.transferFileToRecievers(FtpService.java:44)
        at FtpService.transferSingeFile(FtpService.java:241)
        at FtpService.main(FtpService.java:26)
    Auth fail

源文件中函数transferFileToReciever的部分是

        JSch jsch = new JSch();
        jsch.addIdentity("/root/.ssh/id_dsa");
        Session session = jsch.getSession(username, host, 22);

        session.setUserInfo(serverinfo);
        session.connect(); //geting exception here

        boolean ptimestamp = true;

密码正常,因为我可以使用ssh登录,但是使用JSCh它甚至不提供密钥,用户名和密码。 使用带有java版本“1.6.0_25”的id_dsa密钥。 可能是什么错误?

发现其他类似的问题,但不是答案。 提前谢谢。

7 个答案:

答案 0 :(得分:21)

跟踪根本原因,我终于发现dsa类型的公钥未添加到远程服务器上的授权密钥。附加同样适用于我。

ssh正在使用rsa密钥,导致我回顾我的代码。

谢谢大家。

答案 1 :(得分:3)

示例情况,当我从远程服务器获取文件并将其保存在本地机器中时    包连接器;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class Main {

    public static void main(String[] args) throws JSchException, SftpException, IOException {
        // TODO Auto-generated method stub
        String username = "XXXXXX";
        String host = "XXXXXX";
        String passwd = "XXXXXX";
        JSch conn = new JSch();
        Session session = null;
        session = conn.getSession(username, host, 22);
        session.setPassword(passwd);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();

        ChannelSftp channel = null;
        channel = (ChannelSftp)session.openChannel("sftp");
        channel.connect();

        channel.cd("/tmp/qtmp");

        InputStream in = channel.get("testScp");
        String lf = "OBJECT_FILE";
        FileOutputStream tergetFile = new FileOutputStream(lf);

        int c;
        while ( (c= in.read()) != -1 ) {
            tergetFile.write(c);
        } 

        in.close();
        tergetFile.close();

        channel.disconnect();
        session.disconnect();   

    }

}

答案 2 :(得分:3)

如果用户名/密码包含任何特殊字符,则在camel配置中使用RAW配置

等值
  • RAW(se+re?t&23)其中se+re?t&23是实际密码

  • RAW({abc.ftp.password})其中{abc.ftp.password}值来自spring属性文件。

通过使用RAW,解决了我的问题。

http://camel.apache.org/how-do-i-configure-endpoints.html

答案 3 :(得分:2)

  

发现其他类似的问题,但不是答案。

知道你在哪里找到这个问题会很有趣。

据我所知,根据com.jcraft.jsch.JSchException: Auth cancel 尝试向方法.addIdentity()添加密码。如果生成没有密钥文件的密钥文件,则可以使用""。 另一个错误来源是指纹字符串。如果它不匹配,您将获得身份验证失败(取决于目标服务器)。

最后我的工作源代码 - 在我解决丑陋的管理任务之后:

public void connect(String host, int port, 
                    String user, String pwd,
                    String privateKey, String fingerPrint,
                    String passPhrase
                  ) throws JSchException{
    JSch jsch = new JSch();

    String absoluteFilePathPrivatekey = "./";
    File tmpFileObject = new File(privateKey);
    if (tmpFileObject.exists() && tmpFileObject.isFile())
    {
      absoluteFilePathPrivatekey = tmpFileObject.getAbsolutePath();
    }

    jsch.addIdentity(absoluteFilePathPrivatekey, passPhrase);
    session = jsch.getSession(user, host, port);

    //Password and fingerprint will be given via UserInfo interface.
    UserInfo ui = new UserInfoImpl(pwd, fingerPrint);
    session.setUserInfo(ui);

    session.connect();

    Channel channel = session.openChannel("sftp");
    channel.connect();
    c = (ChannelSftp) channel;
}

答案 4 :(得分:2)

我也面临Auth Fail问题,我的代码问题是我有




  channelSftp.cd(“”);&#xA ;  




将其更改为




  channelSftp.cd(“。”) ;
  




然后就可以了。




答案 5 :(得分:1)

尝试明确添加auth方法,如下所示,因为有时它是必需的:

session.setConfig("PreferredAuthentications", "password");

答案 6 :(得分:0)

在我的情况下,我正在使用以下依赖项

$ valgrind ./bin/person_struct
==2078== Memcheck, a memory error detector
==2078== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==2078== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==2078== Command: ./bin/person_struct
==2078==
        ***PROGRAM TO SORT PERSONS***

Firstname: John
Lastname: Wayne
Age: 91
        ***NEXT PERSON***

Firstname: Jane
Lastname: Doe
Age: 101
        ***NEXT PERSON***

Firstname: Mickey
Lastname: Mouse
Age: 99
        ***NEXT PERSON***


        ***CHOOSE HOW TO SORT***

        By firstname: 1
        By lastname: 2
        By age: 3
        Exit Program: 4

1
        ***SORTING BY FIRSTNAME...***

        ***DONE***

Firstname: Jane | Lastname: Doe | Age: 101
Firstname: John | Lastname: Wayne       | Age: 91
Firstname: Mickey       | Lastname: Mouse       | Age: 99

        ***CHOOSE HOW TO SORT***

        By firstname: 1
        By lastname: 2
        By age: 3
        Exit Program: 4

2
        ***SORTING BY LASTNAME...***

        ***DONE***

Firstname: Jane | Lastname: Doe | Age: 101
Firstname: Mickey       | Lastname: Mouse       | Age: 99
Firstname: John | Lastname: Wayne       | Age: 91

        ***CHOOSE HOW TO SORT***

        By firstname: 1
        By lastname: 2
        By age: 3
        Exit Program: 4

3
        ***SORTING BY AGE...***

        ***DONE***

Firstname: John | Lastname: Wayne       | Age: 91
Firstname: Mickey       | Lastname: Mouse       | Age: 99
Firstname: Jane | Lastname: Doe | Age: 101

        ***CHOOSE HOW TO SORT***

        By firstname: 1
        By lastname: 2
        By age: 3
        Exit Program: 4

4
        ***EXITING PROGRAM***

==2078==
==2078== HEAP SUMMARY:
==2078==     in use at exit: 0 bytes in 0 blocks
==2078==   total heap usage: 3 allocs, 3 frees, 396 bytes allocated
==2078==
==2078== All heap blocks were freed -- no leaks are possible
==2078==
==2078== For counts of detected and suppressed errors, rerun with: -v
==2078== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

并获得相同的Auth异常失败,但是更新了对以下版本的依赖关系并解决了问题。

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
    int testCases, i, j, k, n, num, digits, carry = 0, temp;
    scanf("%d", &testCases);
    int testArr[160];

    for (i = 0; i < testCases; i++)
    {
        scanf("%d", &n);
        num = n;
        if (n == 0 || n == 1)
        {
            testArr[0] = 1;
            digits = 1;
        }
        else
        {
            k = 0;
            for (j = 10; n != 0; j = j * 10)
            {
                testArr[k] = n % j;
                n = n / j;
                k++;
            }
            digits = k;
            for (j = 1; j < num; j++)
            {
                for (k = 0; k < digits; k++)
                {
                    temp = testArr[k] * j + carry;

                    if (temp > 10)
                    {
                        testArr[k] = temp % 10;
                        carry = temp / 10;
                    }
                    else
                    {
                        testArr[k] = temp;
                        carry = 0;
                    }
                }
                if (carry > 10)
                {
                    testArr[k] = carry % 10;
                    k++;
                    testArr[k] = carry / 10;
                    digits = k + 1;
                    carry = 0;
                }
                else if (carry > 0)
                {
                    testArr[k] = carry;
                    digits = k + 1;
                    carry = 0;
                }
            }
        }

        for (k = (digits - 1); k >= 0; k--)
        {
            printf("%d", testArr[k]);
        }
        printf("\n");
    }
    return 0;
}