一些常用的工具方法

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();
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值