2022年8月29日 作者 zeroheart

jsch连接服务器

1.jsch需要搞到0.1.55版本,否则的话会有ssh链接问题,报错ssh algorithm negotiation failed

上述报错有的机器上面ssh配置添加如下内容可以绕过,但有的不行。

Ciphers aes128-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr,3des-cbc,arcfour128,arcfour256,arcfour,blowfish-cbc,cast128-cbc

MACs hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,hmac-sha1-96,hmac-md5-96

KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group1-sha1,[email protected]

2.不要直接使用sshxcute,这个包长期不更新,里面的jsch版本太低为0.1.42,无法正常使用

3.如果是交互式的运行,从控制台输入命令,可以如下操作

JSch jsch = new JSch();
        Session session = jsch.getSession("root", "ip", 22);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
//        sshConfig.put("kex", "diffie-hellman-group1-sha1");
        session.setConfig(sshConfig);
        session.setPassword("pass");
        session.connect();
        ChannelShell channelShell = (ChannelShell) session.openChannel("shell");
        channelShell.setPty(true);
        //使用Window的问题
        channelShell.setInputStream(new FilterInputStream(System.in) {
            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                return in.read(b, off, (len > 1024 ? 1024 : len));
            }
        });
        channelShell.setOutputStream(System.out);
        //去除控制台彩色输出
        channelShell.setPtyType("vt102");
        channelShell.connect(3 * 1000);