ssh2 -java 实例

本文提供了一个使用 Java 实现 SSH2 连接的示例代码,演示了如何建立连接、验证身份并发送简单命令的过程。通过该示例,读者可以了解 SSH2 在 Java 中的基本应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package testcase;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

public class TestSSH2 {
 

    private boolean authed = false;
 
    private Connection conn = null;

    private Session session = null;
   
    public DataInputStream dis = null;

    public DataOutputStream dos = null;
    

    public TestSSH2(String ip, int port) {
        this.conn = new Connection(ip, port);
        try {
            this.conn.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   
    public void auth(String user, String pwd) {
     
        try {
            // true:支持password认证 false:不支持password认证
            if (!this.conn.isAuthMethodAvailable(user, "password")) {
                 System.out.println("---------- 不支持密码认证 ----------");
            }
            this.setAuthed(this.conn.authenticateWithPassword(user, pwd));
            /* true:支持keyboard-interactive认证 false:不支持keyboard-interactive认证
            if (conn.isAuthMethodAvailable(user, "keyboard-interactive")) {
                this.setAuthed(this.conn.authenticateWithKeyboardInteractive(uid, new InteractiveLogic(pwd)));
            }
            */
            // 认证成功
            if (!this.isAuthed()) {
                System.out.println("---------- 认证失败 ----------");
                return;
            }
            System.out.println("---------- 认证成功 ----------");
            this.session = this.conn.openSession();
            this.session.requestPTY("vt100", 80, 800, 0, 0, null);
            this.session.startShell();
            this.dis = new DataInputStream(this.session.getStdout());
            this.dos = new DataOutputStream(this.session.getStdin());
            // 可以尝试输入命令
            //this.session.execCommand("whoami");
            String cmd = "whoami";
            this.dos.write(cmd.getBytes());
            this.dos.write("\r".getBytes());
            Thread.sleep(1000l);
            byte[] buffer = new byte[1024];
            int len = dis.read(buffer);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < len; i++) {
                char c = (char) buffer[i];
                sb.append(c);
            }
            System.out.println(sb.toString());
        } catch(Exception e) {
            e.printStackTrace();
        }
     
    }
 
    public static void main(String[] args) {
        TestSSH2 test = new TestSSH2("192.168.2.99", 22);
        test.auth("root", "Abc@12345");
  
     }

     public boolean isAuthed() {
         return authed;
     }

     public void setAuthed(boolean authed) {
         this.authed = authed;
     }

}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值