使用 JSch 在没有用户名和密码的情况下连接到服务器
Connect to server without username or password using JSch
我正在使用 com.jcraft.jsch.JSch 创建 SFTP 连接。
我的 SSH 密钥已设置为无需用户名或密码即可直接通过 SSH 连接到服务器的位置。
ssh test_server
我的 public 密钥在 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
中的 public 密钥在远程服务器的 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);
}
}
我一直收到
com.jcraft.jsch.JSchException: Auth fail error.
有人知道解决这个问题的方法吗?
附加信息(这可能有用...不确定):
当我打开 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
您没有在 ssh
命令行上明确指定用户名并不意味着没有使用用户名。
ssh
默认情况下隐式使用本地用户名。 JSch 不会那样做。您必须明确指定用户名,即使它与本地用户名相同。
我正在使用 com.jcraft.jsch.JSch 创建 SFTP 连接。
我的 SSH 密钥已设置为无需用户名或密码即可直接通过 SSH 连接到服务器的位置。
ssh test_server
我的 public 密钥在 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
中的 public 密钥在远程服务器的 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);
}
}
我一直收到
com.jcraft.jsch.JSchException: Auth fail error.
有人知道解决这个问题的方法吗?
附加信息(这可能有用...不确定):
当我打开 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
您没有在 ssh
命令行上明确指定用户名并不意味着没有使用用户名。
ssh
默认情况下隐式使用本地用户名。 JSch 不会那样做。您必须明确指定用户名,即使它与本地用户名相同。