package com.ideal.bddt.services.dc.server.hive.operation.impl;
import com.ideal.bddt.services.dc.server.hive.operation.Encryption_HiveService;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.*;
/**
* Created by think on 2018/7/13.
*/
@Service
public class Encryption_HiveServiceImpl implements Encryption_HiveService {
private static Logger logger = LoggerFactory.getLogger("com.ideal.bddt.services.dc.server.ftp.impl.FtpOperationInfoServerImpl");
private static ChannelExec channelExec;
private static Session session = null;
private static int timeout = 60000;
@Override
public String encryptionData(String dataBaseName, String tableName, String fieldName,String partition,String hdf,
String encryptionPartition,String encryptionTime){
try {
String host = "10.142.78.201";
int port = 22;
String host_user = "hadoopadmin";
String host_password = "ideal123";
versouSshUtil(host, host_user, host_password, port);
String command = "sudo sh /home/lixiang/test.sh " + dataBaseName + " " + tableName + " " +fieldName + " "+partition+
" "+hdf+" "+encryptionPartition+" "+encryptionTime;//沙箱
System.out.println(command);
String outStr = runCmd(command, "UTF-8");
System.out.println(outStr);
return "";
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 连接远程服务器
* @param host ip地址
* @param userName 登录名
* @param password 密码
* @param port 端口
* @throws Exception
*/
public static void versouSshUtil(String host,String userName,String password,int port) throws Exception{
System.out.println(11);
JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(userName, host, port); // 根据用户名,主机ip,端口获取一个Session对象
session.setPassword(password); // 设置密码
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config); // 为Session对象设置properties
// session.setTimeout(timeout); // 设置timeout时间
session.connect(); // 通过Session建立链接
System.out.println(2);
System.out.println(session);
}
/**
* 在远程服务器上执行命令
* @param cmd 要执行的命令字符串
* @param charset 编码
* @throws Exception
*/
public static String runCmd(String cmd,String charset) throws Exception{
channelExec = (ChannelExec) session.openChannel("exec");
System.out.println(31);
System.out.println(session);
System.out.println(cmd);
channelExec.setCommand(cmd);
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.connect();
System.out.println(4);
InputStream in = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName(charset)));
String buf = null;
while ((buf = reader.readLine()) != null){
System.out.println(buf);
}
System.out.println(5);
reader.close();
channelExec.disconnect();
System.out.println(6);
return buf;
}
}