当前位置: 首页 > 知识库问答 >
问题:

使用JSch连接到没有用户名或密码的服务器

丁翊歌
2023-03-14

我正在使用com.jcraft.jsch.jsch创建一个SFTP连接。

我的SSH密钥被设置为可以直接SSH到服务器,而不需要用户名或密码。

ssh test_server

我的公钥在me@test_server:~/.SSH/中的authorized_key文件中
我在本地计算机上的SSH密钥在C:\cygwin\home\me\.SSH\
此外,我在此目录中还有一个配置文件和一个known_hosts文件。

Host test_server
User user_name
HostName test_server.domain.com
ForwardAgent yes

为了进行双重检查,我确保C:\cygwin\home\me\.ssh中的公钥在远程服务器的authorized_keys文件中。

我正在开发一个创建文件的GUI程序。有了这个文件,我希望能够将它存储在远程服务器上,并用SFTP将它放在那里的一个文件夹中。我怎么能做到这一点?

以下是我目前掌握的情况:

public static void main(String[] arg) {
    try {
        JSch jsch = new JSch();

        String user = "";
        String host = "test_server";
        int port = 22;
        String privateKey = "C:/cygwin/home/me/.ssh/id_rsa";

        jsch.addIdentity(privateKey);
        System.out.println("identity added ");

        Session session = jsch.getSession("", host);
        System.out.println("session created.");

        session.connect();
        System.out.println("session connected.....");

        Channel channel = session.openChannel("sftp");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect();
        System.out.println("shell channel connected....");

        ChannelSftp c = (ChannelSftp) channel;

        String fileName = "test.txt";
        c.put(fileName, "./in/");
        c.exit();
        System.out.println("done");

    } catch (Exception e) {
        System.err.println(e);
    }
}

我一直收到一个

JSCHException:身份验证失败错误。

有人知道解决这个问题的办法吗?

其他信息(这可能有用...不确定):
当我打开cmd时,它会将我带到C:\users\me
但是当我传递“pwd”命令时,它会打印出/cygdrive/c/users/me
我还可以使用任何新库,或者更好的实现方法。

太感谢您了!

ssh-v test_server的输出:

OpenSSH_7.4p1, OpenSSL 1.0.2k  26 Jan 2017
debug1: Reading configuration data /home/me/.ssh/config
debug1: /home/me/.ssh/config line 1: Applying options for *
debug1: /home/me/.ssh/config line 9: Applying options for wmpos1
debug1: Control socket "/home/me/.ssh/master-user_name@test_server:22" d
oes not exist
debug1: Connecting to test_server [127.0.0.1] port 22.
debug1: Connection established.
debug1: identity file /home/me/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/me/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/me/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/me/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/me/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/me/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/me/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/me/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.2
debug1: match: OpenSSH_6.2 pat OpenSSH* compat 0x04000000
debug1: Authenticating to test_server:22 as 'user_name'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: ecdh-sha2-nistp256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: aes128-ctr MAC: umac-64-etm@openssh.com comp
ression: none
debug1: kex: client->server cipher: aes128-ctr MAC: umac-64-etm@openssh.com comp
ression: none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:aV1o2Laclf7vRU2QOH+iWpuYYmy2
bokk8uQrVEaPL8k
debug1: Host 'test_server' is known and matches the ECDSA host key.
debug1: Found key in /home/me/.ssh/known_hosts:2
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/me/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 151
debug1: Authentication succeeded (publickey).
Authenticated to test_server ([127.0.0.1]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network

共有1个答案

卞经业
2023-03-14

没有在ssh命令行上显式指定用户名这一事实并不意味着没有使用用户名。

ssh默认情况下隐式使用本地用户名。JSch不会那么做的。您必须显式指定用户名,即使它与本地用户名相同。

 类似资料:
  • 在通常的场景中,连接字符串包含纯文本的密码,但这可以被Wireshark捕获,因此我想在一个连接字符串中使用加密的密码。我从Postgres文档中找到了以下摘录:

  • 我想在无需密码验证的情况下通过SFTP服务器上传文件。在这里,我也不想使用私钥。我的公钥已共享到目标SFTP服务器。我可以使用username@hostname作为来自filezilla的url,没有密码或私钥引用。我想从java代码中实现同样的功能。我还研究了j2shh、maverick和jsch API,但每个地方都需要密码或私钥进行身份验证。有什么方法可以通过使用连接到服务器吗usernam

  • 问题内容: 我想连接到MySQL数据库。在安装MySQL时,我没有提供任何密码,因此在我的程序中,我做了相同的操作,但是连接时出现错误。我正在使用属性文件来获取驱动程序,URL,用户名和密码。请帮我。 这是我的代码: 这是我的属性文件内容: 问题答案: -这表示该程序 未 通过任何密码(对于您而言)是正确的。 由于您提到要从属性文件中读取值,因此在发布的代码中看不到您正在执行此操作。如果您确实 从

  • 问题内容: 我正在开发客户端/服务器身份验证程序,但是遇到了问题。客户端可以很好地与服务器建立连接,但是一旦我输入密码和用户名,它就不会返回有效的用户名/密码。如果用户使用正确的用户名/密码服务器登录,则应返回“ Welcome,username”,如果无效,则返回“登录失败”。我查看了printwriter和bufferedreader文档,以确保我使用正确的方法在服务器/客户端之间正确传递文本

  • 我正在构建一个Java Web应用程序,当经理批准他们的请求时,它会自动授予用户对其Windows PC的管理权限。 为了实现这一点,我将编写一个脚本来自动远程访问用户的计算机,并将他们添加到计算机的管理组中。我想为了做到这一点,我需要一个管理服务帐户来访问所有的计算机。 我的问题是,我如何安全地存储管理服务帐户信息?应用程序每次需要访问并授予用户管理员权限时都需要凭据,那么我如何让应用程序在没有