package Util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SomeUtils {
/**
*
* @description 执行一个shell命令
* @cmd 命令
* @filePath 写日志的目录
* @fileName 写日志的文件名
* @author lp
* @date 2016年10月28日 上午10:21:36
*/
public static boolean excuteCmd(String cmd,String filePath,String fileName) {
boolean flag=false;
String[] cmd_arr = {"/bin/sh", "-c", cmd};
Process p;
BufferedReader br = null, ebr = null;
try {
p = Runtime.getRuntime().exec(cmd_arr);
String s;
ebr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = ebr.readLine()) != null) {
writeLogInfo2(s,filePath,fileName);
}
while ((s = br.readLine()) != null) {
writeLogInfo2(s,filePath,fileName);
}
int i = p.waitFor();
if(i==0){
flag=true;
}else{
flag=false;
}
} catch (Exception e) {
e.printStackTrace();
writeLogInfo2(e.toString(),filePath,fileName);
flag=false;
} finally {
try {
if (br != null) {
br.close();
}
if (ebr != null) {
ebr.close();
}
} catch (IOException e) {
e.printStackTrace();
flag=false;
}
}
return flag;
}
//写日志的目录
public static void writeLogInfo2(String info,String filePath,String fileName) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = sdf.format(new Date());
String infoHand = "INFO======>>" + timeStr + "<<====>>" + info
+ "<<======\r\n";
// 判断日志文件
String filePathAll = filePath + fileName;
File fPath = new File(filePath);
if (!fPath.exists()) {
fPath.mkdirs();
}
File file = new File(filePathAll);
if (!file.exists()) {
file.createNewFile();
}
infoToLog(infoHand, filePathAll);
} catch (Exception e1) {
}
}
private synchronized static void infoToLog(String info, String filePathAll) {
try {
FileWriter fw = new FileWriter(filePathAll, true);
fw.write(info);
fw.write("\r\n");
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ssh远程登录 执行命令并返回结果调用过程 是同步的(执行完才会返回)
* @param host 主机名
* @param user 用户名
* @param psw 密码
* @param port 端口
* @param command 命令
* @return
*/
public static int execute(String host,String user,String passwd,String command){
Session session=null;
Channel channel=null;
try {
JSch jsch = new JSch();
session = jsch.getSession(user, host, 22);
session.setPassword(passwd);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
String buf = null;
while ((buf = reader.readLine()) != null)
{
System.out.println(buf);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(!channel.isClosed()&&channel!=null){
channel.disconnect();
}
if(!session.isConnected()&&session!=null){
session.disconnect();
}
}
return channel.getExitStatus();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SomeUtils {
/**
*
* @description 执行一个shell命令
* @cmd 命令
* @filePath 写日志的目录
* @fileName 写日志的文件名
* @author lp
* @date 2016年10月28日 上午10:21:36
*/
public static boolean excuteCmd(String cmd,String filePath,String fileName) {
boolean flag=false;
String[] cmd_arr = {"/bin/sh", "-c", cmd};
Process p;
BufferedReader br = null, ebr = null;
try {
p = Runtime.getRuntime().exec(cmd_arr);
String s;
ebr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = ebr.readLine()) != null) {
writeLogInfo2(s,filePath,fileName);
}
while ((s = br.readLine()) != null) {
writeLogInfo2(s,filePath,fileName);
}
int i = p.waitFor();
if(i==0){
flag=true;
}else{
flag=false;
}
} catch (Exception e) {
e.printStackTrace();
writeLogInfo2(e.toString(),filePath,fileName);
flag=false;
} finally {
try {
if (br != null) {
br.close();
}
if (ebr != null) {
ebr.close();
}
} catch (IOException e) {
e.printStackTrace();
flag=false;
}
}
return flag;
}
//写日志的目录
public static void writeLogInfo2(String info,String filePath,String fileName) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = sdf.format(new Date());
String infoHand = "INFO======>>" + timeStr + "<<====>>" + info
+ "<<======\r\n";
// 判断日志文件
String filePathAll = filePath + fileName;
File fPath = new File(filePath);
if (!fPath.exists()) {
fPath.mkdirs();
}
File file = new File(filePathAll);
if (!file.exists()) {
file.createNewFile();
}
infoToLog(infoHand, filePathAll);
} catch (Exception e1) {
}
}
private synchronized static void infoToLog(String info, String filePathAll) {
try {
FileWriter fw = new FileWriter(filePathAll, true);
fw.write(info);
fw.write("\r\n");
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ssh远程登录 执行命令并返回结果调用过程 是同步的(执行完才会返回)
* @param host 主机名
* @param user 用户名
* @param psw 密码
* @param port 端口
* @param command 命令
* @return
*/
public static int execute(String host,String user,String passwd,String command){
Session session=null;
Channel channel=null;
try {
JSch jsch = new JSch();
session = jsch.getSession(user, host, 22);
session.setPassword(passwd);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
String buf = null;
while ((buf = reader.readLine()) != null)
{
System.out.println(buf);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(!channel.isClosed()&&channel!=null){
channel.disconnect();
}
if(!session.isConnected()&&session!=null){
session.disconnect();
}
}
return channel.getExitStatus();
}
}