JSch执行shell命令

  • 参考:jsch中ChannelShell与ChannelExec区别

参考:jsch中ChannelShell与ChannelExec区别

package com.bob.smalltool;import com.jcraft.jsch.*;
import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import org.apache.log4j.Logger;import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;/*** @program: testjsch* @description: jsh测试类* @author: Mr.Bob* @create: 2019-10-26 21:48**/
public class TestSshSendCommand {private static Logger logger = Logger.getLogger(TestSshSendCommand.class);private JSch jSch;private Session session;private ChannelShell channelShell;private final String ENCODEING = "UTF-8";public static void main(String[] args) {logger.info("开始了");TestSshSendCommand testSshSendCommand = new TestSshSendCommand();try {testSshSendCommand.creatConnection();testSshSendCommand.execCommand();} catch (Exception e) {e.printStackTrace();} finally {//  testSshSendCommand.closeConnection();}}public boolean creatConnection() {logger.info("开始连接了");try {jSch = new JSch();session = jSch.getSession("baibo", "192.168.0.107", 22);session.setConfig("StrictHostKeyChecking", "no");//第一次访问服务器时候不需要输入yessession.setPassword("406963");session.setTimeout(60 * 50 * 1000);session.connect();channelShell = (ChannelShell) session.openChannel("shell");logger.info("session创建成功");} catch (JSchException e) {e.printStackTrace();logger.error("连接失败,请检查是否可以访问:" + e.getMessage());}return false;}public void execCommand() {BufferedReader reader = null;InputStream in = null;OutputStream out = null;logger.info("准备执行命令");List<String> commands = new ArrayList<String>();//commands.add("ls \n\r");//commands.add("cd Desktop \n\r");// commands.add("ls \n\r");commands.add("ifconfig \n\r");commands.add("ping 192.168.0.107 -c 4 \n\r");commands.add("exit /n/r");try {channelShell.connect();//channelShell.setCommand(str);in = channelShell.getInputStream();out = channelShell.getOutputStream();for (String str : commands) {out.write(str.getBytes());out.flush();}reader = new BufferedReader(new InputStreamReader(in, Charset.forName(ENCODEING)));String buf = null;System.out.println(reader.readLine());while ((buf = reader.readLine()) != null) {System.out.println(buf);}} catch (JSchException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public void closeConnection() {if (channelShell != null) {channelShell.disconnect();}if (channelShell != null) {channelShell.disconnect();}if (session != null) {session.disconnect();}}
}

JSch执行shell命令相关推荐

  1. JSch连接不上Linux服务器,windows 下 java程序jsch连接远程linux服务器执行shell命令

    java远程连接服务的shell需要使用SSH的登录方式,可以使用JSch技术.JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等. ...

  2. java连接linux服务器执行shell命令(框架分析+推荐)

    java连接linux服务器执行shell命令(框架分析+推荐) 一.分类+连接方式 程序打成jar包,在本地服务器上执行shell命令.这种使用MyRuntimeUtil工具类 java程序远程li ...

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

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

  4. jenkins 插件执行shell命令时,提示“Command not found”处理方法

    首先提示找不到"Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具 ...

  5. linux c 执行shell命令并获取返回结果

    最近在项目中用到了C语言执行shell命令的问题,调查了一下,一般有system函数.exec族函数,但是还不太清楚怎么获取shell命令执行的返回信息. 例如执行一个ifconfig命令,肯定需要获 ...

  6. vim中执行shell命令小结

    vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如 :!ls - ...

  7. python调用shell命令-在Python中执行shell命令的6种方法,你都知道吗?

    原标题:在Python中执行shell命令的6种方法,你都知道吗? Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库.今天我们就讲解 ...

  8. python调用shell命令-python中执行shell命令的几个方法小结

    最近有个需求就是页面上执行shell命令,第一想到的就是os.system, 复制代码 代码如下: os.system('cat /proc/cpuinfo') 但是发现页面上打印的命令执行结果 0或 ...

  9. python 执行shell命令行效率提升_在python脚本中执行shell命令的方法

    使用Python处理一个shell命令或一个执行一个shell脚本,一般情况下,有以下三种方法,以下我们来看: 第一种方法是使用os.system的方法 os.system(" cmd&qu ...

最新文章

  1. java定义set_java – “方法流()未定义类型Set”
  2. wxWidgets:wxSashEvent类用法
  3. 自定义控件三部曲之动画篇(一)——alpha、scale、translate、rotate、set的xml属性及用法...
  4. 第二节:Css重写样式
  5. 如何把表格做成源代码_他来了,他来了,文字、表格、公式图片识别神器V0.1测试版...
  6. Tomcat绑定jre
  7. python 堆栈_Python堆栈
  8. pyltp环境的搭建
  9. 谈谈工作和学习中,所谓的主动性
  10. linux驱动系列之挂载(转)
  11. Datawhale 202210 Excel | 第五、六、七章 Excel函数示例 Excel函数列表
  12. 图像增强——imhist、imcontour、imadjust、histeq、fspecial、imfilter、medfilt2
  13. 这16个数据可视化案例,惊艳了全球数据行业
  14. 关于Asterisk的传真以及ForIP的概念
  15. 西直门立交桥的破事儿
  16. 写的非常不错的一篇阻塞与非阻塞、同步与异步套接字之间的区别
  17. Win10系统打不开html文件,电脑无法打开html文件
  18. c语言实现七巧板积木拼图大全,七巧板拼图积木的制作方法
  19. 如何成功搭建一个游戏平台?
  20. Sunflower——Google官方的Jetpack学习项目笔记(Java版)

热门文章

  1. 创建自己的手机条形码Thingy
  2. Latex中排序的使用方法
  3. linux文件e属性,三、Linux文件属性
  4. 用容斥原理计算具有有限重数的多重集合的 r-组合(附代码)
  5. P - Balanced Stone Heaps
  6. UVALive 4394 String painter——dp
  7. 自动跟随机器人教程(六)软件部分 树莓派 声源定位
  8. ajax里数组添加数据,小笔记(一):ajax传递数组及将ajax返回数据赋值
  9. MFC CStdioFile简单用法
  10. 浅析销售BOM实施方案及注意事项