先上依赖

<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
<dependency><groupId>ch.ethz.ganymed</groupId><artifactId>ganymed-ssh2</artifactId><version>262</version>
</dependency>

只能一次执行一行命令,遇到要交互的场景就GG了,比如sudo su然后要输入密码

代码demo

package com.springdemo.demo;import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import java.io.*;
import java.nio.charset.Charset;@SpringBootTest
class DemoApplicationTests {@Testvoid contextLoads() {}private String charset = Charset.defaultCharset().toString();private static final int TIME_OUT = 1000 * 5 * 60;@Testpublic void testSsh(){Connection connection = new Connection("192.168.056.105");Session session = null;try {connection.connect();System.out.println("---------------------开始连接------------------------");boolean connectResult = connection.authenticateWithPassword("ubuntu","123456");if(!connectResult){System.out.println("---------连接失败----------");return;}session = connection.openSession();session.execCommand("cp /home/ubuntu/test.txt ./installer/");//接收目标服务器上的控制台返回结果,读取br中的内容;InputStream stdOut = new StreamGobbler(session.getStdout());getStrByInputStream(session,stdOut);//接收目标服务器上的控制台返回结果,读取br中的内容;InputStream stdErrOut = new StreamGobbler(session.getStderr());getStrByInputStream(session,stdErrOut);//当超过 TIME_OUT 中的时候的时候session自动关闭session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);//得到脚本运行成功与否的标志 :0-成功 非0-失败int ret = session.getExitStatus();System.out.println(ret);} catch (IOException e) {e.printStackTrace();}finally {if (session != null) {session.close();}connection.close();}}String getStrByInputStream(Session session ,InputStream inputStream){System.out.println("stdOut:"+inputStream.toString());StringBuffer stringBuffer = new StringBuffer();byte[] bytes = new byte[1024];int result = -1;ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();String str = null;try {while ((result = inputStream.read(bytes, 0, bytes.length)) != -1){byteArrayOutputStream.write(bytes,0,result);}str = new String(byteArrayOutputStream.toByteArray(),charset);byteArrayOutputStream.flush();byteArrayOutputStream.close();} catch (IOException e) {e.printStackTrace();}System.out.println("获取linux的标准输出的结果-----" + str);return str;}}

java使用ganymed-ssh2远程执行shell命令相关推荐

  1. java ganymed ssh2_java 远程执行Shell命令-通过ganymed-ssh2连接

    一.实现远程执行 此程序的目的是执行远程机器上的Shell脚本. [环境参数] 远程机器IP:192.168.243.21 用户名:user 密码:password 命令:python /data/a ...

  2. linux远程执行shell命令行,linux shell 远程执行命令--ftp

    linux shell 远程执行命令--ftp 2018-12-07 ftp有很多命令,熟悉这些命令你能大大的提高工作效率: FTP命令行格式为: ftp -v -d -i -n -g [主机名] , ...

  3. Python ssh 远程执行shell命令

    #工具 python paramiko #远程执行命令 import paramikossh = paramiko.SSHClient() key = paramiko.AutoAddPolicy() ...

  4. 批量远程执行shell命令工具

    使用示例(使用了默认用户root,和默认端口号22): ./mooon_ssh --h=192.168.4.1,192.168.4.2 -P=password -c='cat /etc/hosts' ...

  5. java 远程shell脚本_java通过ssh连接服务器执行shell命令详解及实例

    java通过ssh连接服务器执行shell命令详解 java通过ssh连接服务器执行shell命令:JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转 ...

  6. Ganymed-ssh2实现scp上传和下载文件,以及执行shell命令

    使用Ganymed-ssh2执行远程机器上的Shell脚本,还可以使用SCP来上传和下载文件 严禁转载!!! pom依赖 <dependency><groupId>com.ai ...

  7. python中command是什么意思_python中command执行shell命令脚本方法

    在Python中有一个模块commands也很容易做到以上的效果. 看一下三个函数: 1). commands.getstatusoutput(cmd) 用os.popen()执行命令cmd, 然后返 ...

  8. jenkins 执行shell命令 command not found,make: *** [build] Error 127 解决办法

    本地执行shell命令成功,Jenkins 远程执行 shell命令有时 提示命令找不到,或者make的时候报错. 因为Jenkins执行shell时无法获取环境变量的原因导致 解决办法在shell脚 ...

  9. JSch:纯JAVA实现远程执行SSH2主机的SHELL命令

    http://txy821.iteye.com/blog/1405230 http://my.oschina.net/hetiangui/blog/137426 上篇文章我编写了利用JSch实现SFT ...

最新文章

  1. 第四周项目三-随机数函数应用于游戏(小学生算术能力测试系统)
  2. Linux下编译cscope,vim+ctags+cscope 常用技巧和命令
  3. Failed to execute goal org.apache.maven.plugins:maven-resources-plugin
  4. Eclipse4.5 mars 配置Velocity插件
  5. Redis主从、哨兵模式的搭建
  6. java微信刷卡支付demo_微信刷卡支付例子
  7. JSONObject.toBean方法复制不成功
  8. python编程语言一览_编程语言大汇总(Part Ⅰ)
  9. 前端面试基础题:请写出至少20个HTML5标签
  10. draft.js编辑器开发笔记
  11. R语言 伯努利试验和二项分布
  12. 开源人物之九:赖霖枫
  13. 尼日利亚4g频段_世界各国全球主要4g频段资料
  14. 四、输出 9行9列的99乘法口诀表
  15. 深入理解Serdes 结构 之 CTLE和DFE 技术
  16. 水果店怎样开业吸引人流量,水果店怎样开业吸引人
  17. 【无标题】 R语言下载keras最新方法
  18. 图文解读——为何采购申请PR总是频繁出错
  19. Vue组件选项props
  20. 深度学习模型训练的结果及改进方法

热门文章

  1. 今日头条:任务调度面试题解答
  2. 在线文字转语音Demo(百度)
  3. php 微信支付md5签名,微信支付 MD5 签名中文问题
  4. SD-WAN《夽易联》,开启企业组网新时代
  5. VS2019install插件生成MSI安装包缺少Dll文件的解决方法
  6. [附源码]SSM计算机毕业设计校园跑腿系统JAVA
  7. 20210307 matlab画负频率波特图,负序
  8. 服务器租用哪个IDC服务商比较好
  9. 微信跳一跳辅助php,微信跳一跳辅助python代码实现
  10. 神经网络CNN(学习总结)