Java 继续在JSch中获取Auth fail异常,但我能够在没有密码的情况下通过ssh连接到IP
我试图创建一个JSch隧道,将ssh连接到IP,这是一个运行脚本的无密码设置。当我手动对该IP执行ssh时,我不需要密码Java 继续在JSch中获取Auth fail异常,但我能够在没有密码的情况下通过ssh连接到IP,java,jsch,ssh-tunnel,Java,Jsch,Ssh Tunnel,我试图创建一个JSch隧道,将ssh连接到IP,这是一个运行脚本的无密码设置。当我手动对该IP执行ssh时,我不需要密码card.ipAddress为ssh提供IP地址,这是一种无密码设置。我有log.info(打印)语句,这样我就可以确切地知道我在哪里遇到异常remoteShellScript具有脚本的路径 com.jcraft.jsch.Session jschSession = null; try {
card.ipAddress
为ssh提供IP地址,这是一种无密码设置。我有log.info
(打印)语句,这样我就可以确切地知道我在哪里遇到异常remoteShellScript
具有脚本的路径
com.jcraft.jsch.Session jschSession = null;
try {
log.info("Entering try block of JSch session");
JSch jsch = new JSch();
jschSession = jsch.getSession(USERNAME, card.ipAddress, REMOTE_PORT);
log.info("after getSession");
// not recommend, uses jsch.setKnownHosts
jschSession.setConfig("StrictHostKeyChecking", "no");
log.info("after setting Config");
// authenticate using password
//jschSession.setPassword(PASSWORD);
// connect timeout session
jschSession.connect(); //This is where I get the Exception
log.info("after connecting to jschSession");
ChannelExec channelExec = (ChannelExec) jschSession.openChannel("exec");
log.info("Channel is open");
// run a shell script
channelExec.setCommand("sh " + remoteShellScript + "\"" + cn_ip + "\" " + duration);
log.info("after running script");
// display errors to System.err
channelExec.setErrStream(System.err);
log.info("after setErr");
//InputStream in = channelExec.getInputStream();
// 5 seconds timeout channel
channelExec.connect();
log.info("after connect");
} catch (Exception e) {
log.error("Catching exception", e);
e.printStackTrace();
} finally {
log.info("Disconnecting session from Finally block");
if (jschSession != null) {
jschSession.disconnect();
}
例外情况:
Catching exception: com.jcraft.jsch.JSchException: Auth fail
Jsch
需要您的密钥才能进行身份验证
之后:
JSch jsch = new JSch();
加:
“无密码”是指您使用密钥进行身份验证,还是指没有密码?前一段时间有一个问题,在不需要密码的情况下,比较密钥在
~/.ssh/id\u rsa.pub
中,我应该以jsch.addIdentity(“~/.ssh/id\u rsa.pub”)
或jsch.addIdentity(.ssh/id\u rsa.pub”)
的形式输入它吗。当我打开.pub文件时,我看到ssh rsa agdgsome随机字符root@some.host.name“那么,我是不是应该把整个东西作为一把钥匙?或者这些随机字符是私钥?您需要写入私钥的路径。密钥必须已经在运行代码的系统上的某个位置(因为您可以手动连接而不会出现问题)。是的,“~/.ssh/id\u rsa.pub”
和”。ssh/id\u rsa.pub“
是否考虑了路径?我不知道是否在开头包含~/
;如果您使用$HOME中的working dir运行程序,通常情况下,您可以将目录保留在路径名(2)之外,正如Timon所说,您需要提供私钥文件——即“path/.ssh/id\u rsa”
而不是“path/.ssh/id\u rsa.pub”
。
jsch.addIdentity( "path/to/your/key" );