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: 

相关内容

热门资讯

WPK外挂作弊!hhpoker... WPK外挂作弊!hhpoker底牌透视脚本,淘宝买wepoker透视有用吗(真是真的是有挂)1、首先...
WPK外挂作弊!德普之星辅助工... WPK外挂作弊!德普之星辅助工具如何设置,wepoker有没有挂(一直真的是有挂)德普之星辅助工具如...
WePoKer外挂作弊!菠萝德... WePoKer外挂作弊!菠萝德州透视脚本,we poker插件(切实有挂)1、菠萝德州透视脚本免费脚...
WPK外挂作弊!德州来玩辅助器... WPK外挂作弊!德州来玩辅助器,微扑克辅助下载(好像真的有挂)1、让任何用户在无需德州来玩辅助器安装...
我来向大家传授!丽水都莱脚本(... 我来向大家传授!丽水都莱脚本(专业辅助教程)确实有透视绝活丽水都莱脚本能透视中分为三种模型:丽水都莱...
WePoKer外挂作弊!wep... WePoKer外挂作弊!wepoker轻量版辅助,佛手大菠萝13道挂哪里(都是是有挂)1、金币登录送...
微扑克外挂作弊!epoker透... 微扑克外挂作弊!epoker透视底牌,wepoker透视脚本免费下载(本来存在有挂)1)wepoke...
总算了解!多乐跑如何增加胜率吧... 总算了解!多乐跑如何增加胜率吧(解密辅助挂)原来有透视窍要多乐跑如何增加胜率吧透视方法中分为三种模型...
玩家科普!闲逸游戏辅助软件下载... 玩家科普!闲逸游戏辅助软件下载(详情辅助挂)一贯有透视技法1、游戏颠覆性的策略玩法,独创攻略技巧玩法...
wpk外挂作弊!约局吧能不能开... wpk外挂作弊!约局吧能不能开挂,hhpoker透视脚本视频(竟然是真的有挂)hhpoker透视脚本...