11

使用 SSHJ 时出现异常。

以下是我的实现方式:

public static void main(String[] args) throws IOException { 
    // TODO Auto-generated method stub 
    final SSHClient ssh = new SSHClient(); 
    ssh.loadKnownHosts(); 
    ssh.connect("serverName"); 
    try{ 
        ssh.authPublickey("myUserId"); 
        final Session session = ssh.startSession(); 
        try{ 
            final Command cmd = session.exec("net send myMachineName Hello!!!"); 
            System.out.println(cmd.getOutputAsString()); 
            System.out.println("\n Exit Status: "+cmd.getExitStatus()); 
        }finally{ 
            session.close(); 
        } 
        }finally{ 
            ssh.disconnect(); 
        }    
    } 

} 

但我得到以下异常:

Exception in thread "main" java.io.IOException: Could not load known_hosts
    at net.schmizz.sshj.SSHClient.loadKnownHosts(SSHClient.java:528)
    at SSHTEST.main(SSHTEST.java:25)

我究竟做错了什么?

4

3 回答 3

15

使用以下代码

final SSHClient ssh = new SSHClient();  

ssh.addHostKeyVerifier(  
    new HostKeyVerifier() {  
        public boolean verify(String arg0, int arg1, PublicKey arg2) {  
            return true;  // don't bother verifying  
        }  
    }  
);  

ssh.connect("LocalHost");
于 2011-02-26T12:56:16.733 回答
7

删除对 loadKnownHosts() 方法的调用,正如 erickson 提到的那样,默认情况下会在 ~/.ssh/known_hosts 下检查(您也可以将位置指定为参数),并将其替换为:

ssh.addHostKeyVerifier("public-key-fingerprint");

要找出指纹是什么,扭曲的方法是在没有该语句的情况下进行连接 - 你会从异常中找到;-)

于 2010-09-03T22:34:02.513 回答
3

听起来它正在尝试读取“known_hosts”文件,但找不到它,或者它的格式可能无效。

SSH 已知主机文件记录了各种主机的公钥,以阻止一些欺骗攻击。通常它位于 ~/.ssh/known_hosts 中。尝试在那里创建一个空文件,看看是否满足库。

库文档可能会解决必要的配置文件。

于 2010-09-02T19:21:59.220 回答