“无法加载known_hosts”使用 SSHJ 的异常

发布于 2024-09-16 17:52:27 字数 978 浏览 10 评论 0原文

我在使用 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)

我做错了什么?

I am getting an exception while using SSHJ.

Here is how I implemented it:

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(); 
        }    
    } 

} 

But I get the following exception:

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)

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹雪碎 2024-09-23 17:52:27

使用以下代码

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");

Use the folowing code

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");
不喜欢何必死缠烂打 2024-09-23 17:52:27

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

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

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

Remove the call to loadKnownHosts() method, which as erickson mentioned checks under ~/.ssh/known_hosts by default (you can specify the location as an argument as well though), and replace it with:

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

To find out what the fingerprint is, the twisted way would be to connect without that statement - you'll find out from the exception ;-)

南薇 2024-09-23 17:52:27

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

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

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

It sounds like it's trying to read a "known_hosts" file, but can't find it, or possibly it in an invalid format.

The SSH known hosts file records the public key for various hosts to thwart some spoofing attacks. Normally it resides in ~/.ssh/known_hosts. Try creating an empty file there and see if that satisfies the library.

The library documentation is likely to address the necessary configuration files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文