JSch执行shell命令

参考: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");//第一次访问服务器时候不需要输入yes
            session.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();
        }

    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值