org.apache.sshd的SshClient客户端 连接服务器执行命令 示例
创始人
2025-01-18 09:33:20
0次

引入依赖

		             org.apache.sshd             sshd-core             2.9.1          

示例代码,可以直接执行,也可以做替换命令、维护session等修改

package com.demo.demo;  import lombok.extern.slf4j.Slf4j; import org.apache.sshd.client.SshClient; import org.apache.sshd.client.channel.ChannelExec; import org.apache.sshd.client.channel.ClientChannelEvent; import org.apache.sshd.client.future.AuthFuture; import org.apache.sshd.client.future.ConnectFuture; import org.apache.sshd.client.session.ClientSession;  import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.EnumSet; import java.util.concurrent.TimeUnit;  @Slf4j public class SshdUtil {      public static final String WHO_AM_I = "whoami";      public static void main(String[] args) {         execute("**.**.**.**",                 22,                 "root",                 "*****");     }      private static void execute(String host, int port, String username, String password) {         SshClient client = SshClient.setUpDefaultClient();         ClientSession session = null;         try {             client.start();             ConnectFuture connectFuture = client.connect(username, host, port).verify(10, TimeUnit.SECONDS);             if (connectFuture.isConnected()) {                 session = connectFuture.getSession();                 session.addPasswordIdentity(password);                 AuthFuture auth = session.auth().verify(10, TimeUnit.SECONDS);                 if (auth.isSuccess()) {                     channelCmd(session, WHO_AM_I);                 }             }         } catch (IOException e) {             log.error("执行异常", e);         } finally {             try {                 if (session != null) {                     session.close();                 }                 client.close();             } catch (IOException e) {                 log.error("关闭异常", e);             }         }     }      private static void channelCmd(ClientSession session, String cmd) throws IOException {         ChannelExec channel = session.createExecChannel(cmd);         ByteArrayOutputStream output = new ByteArrayOutputStream();         ByteArrayOutputStream outputErr = new ByteArrayOutputStream();         channel.setOut(output);         channel.setErr(outputErr);         channel.open();         channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 0);         log.info("Output:{}", output.toString().replace("\n", "\\n"));         log.info("Error:{}", outputErr.toString().replace("\n", "\\n"));     } }  

输出如下,分别代表了输出流stdout和错误输出流stderr

*** [main] SshdUtil INFO  -Output:root\n *** [main] SshdUtil INFO  -Error: 

相关内容

热门资讯

微扑克辅助器!德州平台有没有挂... 您好:德州平台有没有挂这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
AApoker透视挂!wepo... 很多人使用云端 AI 时,都担心wepoker透视挂是真的吗、内部资料上传带来隐私风险。这款本地运行...
WePoKer透视!wepok... WePoKer透视!wepoker如何拿到好牌,分享透视多功能软件APP模板;近期一款 AI 黑科技...
红龙扑克透视挂!德扑之星规律,... 红龙扑克透视挂!德扑之星规律,解迷透视挂多功能插件APP法门是一款可以让一直输的玩家,快速成为一个“...
aapoker透视!WePoK... 您好,WePoKer有没有透视软件挂这款游戏可以开挂的,确实是有挂的,需要了解加微【48527505...
红龙扑克开挂!微扑克的辅助工具... 很多人使用云端 AI 时,都担心微扑克的辅助工具有哪些、内部资料上传带来隐私风险。这款本地运行的 A...
aapoker辅助!wepok... 您好:wepoker私人局怎么设置抽水这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会...
aapoker辅助挂!wepo... aapoker辅助挂!wepoker透视挂安全吗,普及辅助挂多功能方法APP策略AI 智能体黑科技再...
红龙PoKer辅助挂!wepo... 您好:wepokerplus透视挂真的假的这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌...
德扑之星辅助挂!德州ai辅助有... 德扑之星辅助挂!德州ai辅助有用吗,详细辅助挂多功能下载APP指南书是一款可以让一直输的玩家,快速成...