Parcourir la source

hos-admin shell 删除

demon il y a 8 ans
Parent
commit
0fbf334fe2

+ 27 - 33
src/main/java/com/yihu/hos/remoteManage/controller/RemoteShellController.java

@ -1,8 +1,6 @@
package com.yihu.hos.remoteManage.controller;
import com.jcraft.jsch.Session;
import com.yihu.hos.remoteManage.service.RemoteShellService;
import com.yihu.hos.remoteManage.service.SSHLinuxTool;
import com.yihu.hos.web.framework.util.controller.BaseController;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
@ -28,10 +26,6 @@ public class RemoteShellController extends BaseController {
    @Resource(name = RemoteShellService.BEAN_ID)
    private RemoteShellService remoteShellService;
    @Resource(name = SSHLinuxTool.BEAN_ID)
    private SSHLinuxTool sshLinuxTool;
    public static Session session ;
    /**
     * 远程shell操作页面
@ -68,33 +62,33 @@ public class RemoteShellController extends BaseController {
        return result;
    }
    //连接操作shell测试
    @RequestMapping(value = "/shell", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public String shellTest(String cmd, String disCon) {
        String result = "";
        try {
            if (session==null){
                session = sshLinuxTool.getsessionConn();
            }
            if ("false".equals(disCon)) {
                //保持通道连接
                System.out.println("循环开始1111111==================");
                result = sshLinuxTool.sshShell(session, cmd, false);
                System.out.println("结果:"+result);
            } else {
                //断开通道连接,会话
                System.out.println("循环开始2222222222==================");
                result = sshLinuxTool.sshShell(session, cmd, true);
                session = null;
                System.out.println("结果:"+result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
//
//    //连接操作shell测试
//    @RequestMapping(value = "/shell", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
//    @ResponseBody
//    public String shellTest(String cmd, String disCon) {
//        String result = "";
//        try {
//            if (session==null){
//                session = sshLinuxTool.getsessionConn();
//            }
//            if ("false".equals(disCon)) {
//                //保持通道连接
//                System.out.println("循环开始1111111==================");
//                result = sshLinuxTool.sshShell(session, cmd, false);
//                System.out.println("结果:"+result);
//            } else {
//                //断开通道连接,会话
//                System.out.println("循环开始2222222222==================");
//                result = sshLinuxTool.sshShell(session, cmd, true);
//                session = null;
//                System.out.println("结果:"+result);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return result;
//    }
}

+ 0 - 122
src/main/java/com/yihu/hos/remoteManage/service/SSHLinuxTool.java

@ -1,122 +0,0 @@
package com.yihu.hos.remoteManage.service;
import com.jcraft.jsch.*;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * ssh 链接操作测试类
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/1/3.
 */
@Service("SSHLinuxTool")
public class SSHLinuxTool {
    public static final String BEAN_ID = "SSHLinuxTool";
    public static String host = "172.19.103.57";
    public static int port = 22;
    public static String user = "root";
    public static String password = "ceshi";
    public static Channel channel = null;
    /**
     * //TODO 密钥密码等验证
     * 建立一个shell连接会话
     *
     * @return
     * @throws JSchException
     */
    public Session getsessionConn() throws JSchException {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setConfig("StrictHostKeyChecking", "no");
        //    java.util.Properties config = new java.util.Properties();
        //   config.put("StrictHostKeyChecking", "no");
        session.setTimeout(600000); // 设置timeout时间
        session.setPassword(password);
        session.connect();
        return session;
    }
    public String exeCommand(Session session, String command, boolean exit) throws JSchException, IOException {
        if (!session.isConnected()) {
            session = getsessionConn();
        }
        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        InputStream in = channelExec.getInputStream();
        channelExec.setCommand(command);
        channelExec.setErrStream(System.err);
        channelExec.connect();
        String out = IOUtils.toString(in, "UTF-8");
        if (exit) {
            channelExec.disconnect();
        }
        return out;
    }
    /**
     * 利用JSch实现远程主机SHELL命令执行
     */
    public static String sshShell(Session session, String command, boolean exit) throws Exception {
        //如果服务器连接不上,则抛出异常
        if (session == null) {
            throw new Exception("session is null");
        }
        try {
            //创建sftp通信通道
            if (channel == null) {
                channel = session.openChannel("shell");
                channel.connect(1000);
            }
            //获取输入流和输出流
            InputStream instream = channel.getInputStream();
            OutputStream outstream = channel.getOutputStream();
            //发送需要执行的SHELL命令,需要用\n结尾,表示回车
            String shellCommand = command + " \n";
            outstream.write(shellCommand.getBytes());
            outstream.flush();
            //获取命令执行的结果
            Thread.sleep(2000);
            if (instream.available() > 0) {
                byte[] data = new byte[instream.available()];
                int nLen = instream.read(data);
                if (nLen < 0) {
                    throw new Exception("network error.");
                }
                //转换输出结果并打印出来
                String temp = new String(data, 0, nLen, "iso8859-1");
                System.out.println(temp);
                return temp;
            }
            outstream.close();
            instream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (exit) {
                channel.disconnect();
                session.disconnect();
            }
        }
        return null;
    }
}