|  | @ -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;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |