Apache MINA SSHDSecure Shell Daemon是基于Apache MINAMultipurpose Infrastructure for Network Applications开发的一个开源的Java库专门用于提供SSHSecure Shell服务。SSH是一种网络协议用于在不安全的网络环境中进行安全通信和远程操作主要用于远程登录、文件传输、以及安全的命令执行等场景。import org.apache.sshd.client.SshClient; import org.apache.sshd.client.channel.ChannelShell; import org.apache.sshd.client.session.ClientSession; import java.io.PrintStream; public class SSHClient { public static void main(String[] args) { String ip 127.0.0.1; int port 22; String username root; String password ***; ClientSession session null; ChannelShell channel null; PrintStream out null; Thread thread null; try { SshClient client SshClient.setUpDefaultClient(); client.start(); session client .connect(username, ip, port) .verify(3000) .getSession(); session.addPasswordIdentity(password); session.auth().verify(3000); session.resetAuthTimeout(); channel session.createShellChannel(); channel.setPtyType(xterm); channel.open().verify(6000); if (channel.isOpen()) { System.out.println(SSH 连接成功); } thread new Thread(new SSHClientThread(channel)); thread.start(); channel.getInvertedIn().write(ls.getBytes()); channel.getInvertedIn().flush(); Thread.sleep(10 *1000); } catch (Exception e) { e.printStackTrace(); } finally { try { if (session ! null) { session.close(); } } catch (Exception e) { } try { if (channel ! null) { channel.close(); } } catch (Exception e) { } try { if (out ! null) { out.close(); } } catch (Exception e) { } try { if (thread ! null) { thread.stop(); } } catch (Exception e) { } } } } import org.apache.sshd.client.channel.ChannelShell; import java.io.InputStreamReader; public class SSHClientThread implements Runnable { public ChannelShell channel; public SSHClientThread(ChannelShell channel){ this.channel channel; } Override public void run() { try (InputStreamReader reader new InputStreamReader(channel.getInvertedOut())) { char[] tmp new char[1024]; int i 0; while ((i reader.read(tmp, 0, 1024)) ! -1) { System.out.println(收到消息String.valueOf(tmp, 0, i)); } } catch (Exception exception) { exception.printStackTrace(); } } }参考文章Apache MINA SSHD-CSDN博客