Java获取linux中命令结果_java通过Linux获取命令信息并显示出来

package test;

import java.io.*;

import java.nio.charset.Charset;

import java.util.Properties;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

import org.apache.commons.io.IOUtils;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

/**

* @author lw

* @createTime 2018/8/3 15:49

* @description ssh登陆主机 输入密码登陆并执行命令

*/

public class LinuxShh {

private static Logger logger = LogManager.getLogger(LinuxShh.class);

final static String userName = "test";// 用户名

final static String password = "87654321";// 密码

final static String host = "174.31.514.222";// 服务器地址

final static int port = 22;// 端口号

final static int timeout = 60000000;

/**

*

* com.jcraft

* jsch

* 0.1.54

*

*/

public static void main(String[] args) {

String cmd = "uname -a && date && uptime && who && vmstat 1 60 ";

/*try {

String rs = LinuxShh.exeCommand(host,port,userName,password,cmd);

System.out.println(rs);

} catch (JSchException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}*/

try {

LinuxShh.getLog(host,port,userName,password,cmd);

} catch (JSchException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

*@Author:lw

*@Description: 实时获取命令日志

* @param :host,port user,password cmmmand

*@Date:14:00 2018/8/6

*/

public static void getLog(String host, int port, String user, String password, String command)throws JSchException, IOException{

JSch jsch = new JSch(); // 创建JSch对象

//String cmd = "vmstat 1 1";// 要运行的命

String cmd = "uname -a && date && uptime && vmstat 1 60 "; //>/home/linux_shell/m/vmstat.txt

Session session = jsch.getSession(user, 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建立链接

ChannelExec channelExec = (ChannelExec) session.openChannel("exec");

channelExec.setCommand(command);

channelExec.setInputStream(null);

channelExec.setErrStream(System.err);

channelExec.connect();

InputStream in = channelExec.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));

//写入相应的文件

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\2.txt"),"UTF-8"));

String buf = null;

StringBuffer sb = new StringBuffer();

System.out.println("您的IP是:" + host);

logger.info("您的IP是:" + host);

System.out.println("以下是:系统资源信息:");

while ((buf = reader.readLine()) != null) {

sb.append(buf);

//写入相关文件

out.write(buf);

out.newLine();

System.out.println(buf);// 打印控制台输出

}

//清楚缓存

out.flush();

//关闭流

reader.close();

out.close();

channelExec.disconnect();

if (null != session) {

session.disconnect();

}

}

/**

*@Author:lw

*@Description: 去除全部结果后,才显示处理,不是实时获取数据

*@Date:13:53 2018/8/6

*/

public static String exeCommand(String host, int port, String user, String password, String command) throws JSchException, IOException {

JSch jsch = new JSch();

Session session = jsch.getSession(user, host, port);

session.setConfig("StrictHostKeyChecking", "no");

session.setPassword(password);

session.connect();

session.setTimeout(timeout); // 设置timeout时间

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");

channelExec.disconnect();

session.disconnect();

return out;

}

}

结果:

e4c16e0f510c3ec4646d06fafaf0644b.png

原文:http://blog.51cto.com/357712148/2155239

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值