我一直花費相當多的時間試圖讓JSCH正常工作。我試圖讓它進入SSH主機並運行一個命令(basically sudo su; cd /dir/dir/dir ; sh bashscript.sh
),但是,似乎我每次都被「Auth Failed」所困擾。JSCH SSH給我不斷驗證驗證碼
我已經啓用JSCH記錄:
INFO: Connecting to {HOST} port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_5.3
INFO: Local version string: SSH-2.0-JSCH-0.1.53
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: aes256-ctr is not available.
INFO: aes192-ctr is not available.
INFO: aes256-cbc is not available.
INFO: aes192-cbc is not available.
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO: kex: server: ssh-rsa,ssh-dss
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
INFO: kex: server: hmac-md5,hmac-sha1,[email protected],hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
INFO: kex: server: hmac-md5,hmac-sha1,[email protected],hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
INFO: kex: server: none,[email protected]
INFO: kex: server: none,[email protected]
INFO: kex: server:
INFO: kex: server:
INFO: kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
INFO: kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: none
INFO: kex: client: none
INFO: kex: client:
INFO: kex: client:
INFO: kex: server->client aes128-ctr hmac-md5 none
INFO: kex: client->server aes128-ctr hmac-md5 none
INFO: SSH_MSG_KEXDH_INIT sent
INFO: expecting SSH_MSG_KEXDH_REPLY
INFO: ssh_rsa_verify: signature true
WARN: Permanently added '{HOST}' (RSA) to the list of known hosts.
INFO: SSH_MSG_NEWKEYS sent
INFO: SSH_MSG_NEWKEYS received
INFO: SSH_MSG_SERVICE_REQUEST sent
INFO: SSH_MSG_SERVICE_ACCEPT received
INFO: Disconnecting from {HOST} port 22
com.jcraft.jsch.JSchException: Auth fail
我不明白爲什麼在世界上似乎實際連接到主機,將其添加到RSA,然後返回驗證失敗。
這裏是ssh.java文件:
public class Ssh {
private String USERNAME = "USERNAME";
public void connect(String remoteHost, int remotePort, String cmd) throws JSchException, InterruptedException {
try {
JSch.setLogger(new debugLogger());
JSch jSch = new JSch();
Session session = jSch.getSession(USERNAME, remoteHost, remotePort);
session.setConfig("PreferredAuthentications", "privatekey");
jSch.setKnownHosts("~/.ssh/known_hosts");
jSch.addIdentity("~/.ssh/id_rsa");
session.setConfig("StrictHostKeyChecking", "no");
session.connect(30000);
Channel channel = session.openChannel("exec");
ChannelExec channelExec = (ChannelExec) channel;
channelExec.setCommand(cmd);
channelExec.setErrStream(System.err);
BufferedReader reader = new BufferedReader(new InputStreamReader(channelExec.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
channelExec.disconnect();
session.disconnect();
System.out.println("Exit Code: " + channelExec.getExitStatus());
} catch (JSchException|IOException e) {
System.out.println(e);
}
}
private static class debugLogger implements Logger{
static Hashtable name = new Hashtable();
static{
name.put(new Integer(DEBUG), "DEBUG: ");
name.put(new Integer(INFO), "INFO: ");
name.put(new Integer(WARN), "WARN: ");
name.put(new Integer(ERROR), "ERROR: ");
name.put(new Integer(FATAL), "FATAL: ");
}
public boolean isEnabled(int level){
return true;
}
public void log(int level, String message){
System.err.print(name.get(new Integer(level)));
System.err.println(message);
}
}
}
'jSch.addIdentity( 「的〜/ .ssh/id_rsa」);'請問jsch明白'〜'? – Kenster
通過它,看起來確實如此。 我想問題是,是否應該'id_rsa'路徑指向我正在運行的計算機上的路徑或服務器端?我會假設機器本身(IE:從'localhost'運行,我應該路徑到'/Users/localhostusername/.ssh/id_rsa',而不是'/ home/serveruser/.ssh/id_rsa') – user249656
似乎甚至提供實際路徑只是'USERAuth失敗'的結果[ – user249656