今天周六日,想起了我过往的岁月,历经此生,酒满贪杯人不醉,写上一工具类,生产可用。

包括:

1.执行本地shell命令,并打印出日志结果

2.执行远程shell命令,并打印出日志结果

依赖于jsch库

 

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.52</version>
</dependency>

 

 

package com.dev.util.ftp;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.IOUtils;

import com.dev.util.log.Log4jUtil;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**

* <p>
* Title: java调用Linux下的shell命令工具
* </p>
* <p>
* Description:
* </p>

* @作者 Andy
* @创建时间 2016-10-15
* @版本 1.00
* @修改记录

* <pre>
* 版本 修改人 修改时间 修改内容描述
* ----------------------------------------
* 1.00 Andy 2016-10-15 
* ----------------------------------------
* </pre>
*/
public class ShellUtil {

// 基本路径
private static final String basePath = "/nas/arfp/";

// 记录Shell执行状况的日志文件的位置(绝对路径)
private static final String executeShellLogFile = basePath
+ "executeShell.log";

/**
* \

* @param shellCommand
* @return
* @throws IOException
*/
public static String executeShell(String shellCommand) throws IOException {
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = null;
BufferedReader errorReader = null;
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");

StringBuffer result = new StringBuffer();

try {
stringBuffer.append(dateFormat.format(new Date())).append(
"准备执行Shell命令 ").append(shellCommand).append(" \r\n");

Process process = null;
String[] cmd = { "/bin/sh", "-c", shellCommand };
// 执行Shell命令
process = Runtime.getRuntime().exec(cmd);
if (process != null) {
stringBuffer.append("进程号:").append(process.toString()).append(
"\r\n");
// bufferedReader用于读取Shell的输出内容
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()), 1024);
errorReader = new BufferedReader(new InputStreamReader(process
.getErrorStream()), 1024);
process.waitFor();
} else {
stringBuffer.append("没有pid\r\n");
}
stringBuffer.append(dateFormat.format(new Date())).append(
"Shell命令执行完毕\r\n执行结果为:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while (bufferedReader != null
&& (line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
result.append(line);
}

while (errorReader != null
&& (line = errorReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
result.append("execute fail:");
result.append(line);
}
} catch (Exception ioe) {
stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage())
.append("\r\n");
} finally {
if (bufferedReader != null || errorReader != null) {
OutputStreamWriter outputStreamWriter = null;
try {
// 将Shell的执行情况输出到日志文件中
OutputStream outputStream = new FileOutputStream(
executeShellLogFile, true);
outputStreamWriter = new OutputStreamWriter(outputStream,
"UTF-8");
outputStreamWriter.write(stringBuffer.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStreamWriter.close();
}
}

if (bufferedReader != null) {
IOUtils.closeQuietly(bufferedReader);
}

if (errorReader != null) {
IOUtils.closeQuietly(errorReader);
}
}

return result.toString();
}


/**
* 利用JSch包实现远程主机SHELL命令执行
* @param ip 主机IP
* @param user 主机登陆用户名
* @param psw 主机登陆密码
* @param port 主机ssh2登陆端口,如果取默认值,传-1
* @param privateKey 密钥文件路径
* @param passphrase 密钥的密码
*/
public static void sshShell(String ip, String user, String psw
,int port ,String privateKey ,String passphrase) throws Exception{
Session session = null;
Channel channel = null;


JSch jsch = new JSch();

//设置密钥和密码
if (privateKey != null && !"".equals(privateKey)) {
if (passphrase != null && "".equals(passphrase)) {
//设置带口令的密钥
jsch.addIdentity(privateKey, passphrase);
} else {
//设置不带口令的密钥
jsch.addIdentity(privateKey);
}
}

if(port <=0){
//连接服务器,采用默认端口
session = jsch.getSession(user, ip);
}else{
//采用指定的端口连接服务器
session = jsch.getSession(user, ip ,port);
}

//如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("session is null");
}

//设置登陆主机的密码
session.setPassword(psw);//设置密码 
//设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking", "no");
//设置登陆超时时间 
session.connect(30000);

try {
//创建sftp通信通道
channel = (Channel) session.openChannel("shell");
channel.connect(1000);

//获取输入流和输出流
InputStream instream = channel.getInputStream();
OutputStream outstream = channel.getOutputStream();

//发送需要执行的SHELL命令,需要用\n结尾,表示回车
String shellCommand = "ls \n";
outstream.write(shellCommand.getBytes());
outstream.flush();


//获取命令执行的结果
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,"UTF-8");
String tempData=new String(data, "UTF8");
// System.out.println(temp);
Log4jUtil.info(tempData);
}
outstream.close();
instream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
channel.disconnect();
}
}
/**

* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

// String fileName = args[0];

// 调用shell命令上传文件
String cmd = "aftstcp 1 ABISFILE %s 01@000";
// String result = ShellUtil
// .executeShell(String.format(cmd, fileName));
ShellUtil.sshShell("ip", "用户名", "密码", 22, null, null);
// System.out.println("dataFileUploadResult = " + result);
// if (result != null && result.indexOf("RC: -1") != -1) {
// System.out.println("upload file success");
// }
}
}