Jsch-Useage

Jsch是一个纯Java实现的Shell通信类库

简易Demo

  • 在服务器上执行ls命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    JSch jSch = new JSch();
    val session = jSch.getSession("root", "192.168.0.2", 22);
    session.setPassword("123456");
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect();
    ChannelExec exec = (ChannelExec) session.openChannel("exec");
    exec.setCommand("ls");
    exec.connect(3 * 1000);
    System.out.println(StreamUtils.copyToString(exec.getInputStream(), Charset.defaultCharset()));
    exec.disconnect();
    session.disconnect();
  • 输出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    client_linux_amd64
    dead.letter
    go
    jdk-8u172-linux-x64.rpm
    jstatd.all.policy
    kcptun-linux-amd64-20180316.tar.gz
    server_linux_amd64
    ss_kcp.sh
    VirtualBox VMs
    v_kcp.sh