java远程调用linux文件内容_Java远程实现Linux文件内容读取

//首先要下载com.jcraft.jsch_0.1.31.jar

package Test;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

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;

/**

*

* java 远程读取linux文件

* @author zhaochong

*/

public class ShellUtil {

private static JSch jsch;

private static Session session;

/**配置连接

* @param user

* @param passwd

* @param host

* @param post

* @throws Exception

*/

public static void connect(String user, String passwd, String host,int post) throws Exception {

jsch = new JSch();

session = jsch.getSession(user, host, post);

if (session == null) {

throw new Exception("session is null");

}

session.setPassword(passwd);

java.util.Properties config = new java.util.Properties();

//第一次登陆

config.put("StrictHostKeyChecking", "no");

session.setConfig(config);

try {

session.connect(30000);

} catch (Exception e) {

throw new Exception("连接远程端口无效或用户名密码错误");

}

}

public static String[] getArr(String str){

String[] arr = str.split(":");

return arr;

}

/**

* @param command shell 命令

* @param user 用户名

* @param passwd 密码

* @param host ip地址

* @param post 端口号

* @throws Exception

*/

public static void execCmd(String command, String user, String passwd, String host,int post) throws Exception {

System.out.println(command);

connect(user, passwd, host,post);

BufferedReader reader = null;

Channel channel = null;

try {

channel = session.openChannel("exec");

((ChannelExec) channel).setCommand(command);

channel.setInputStream(null);

((ChannelExec) channel).setErrStream(System.err);

channel.connect();

InputStream in = channel.getInputStream();

reader = new BufferedReader(new InputStreamReader(in));

String buf = null;

List list = new ArrayList();

//返回数据

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

String[] arr = buf.split("\|");

for (int i = 0; i < arr.length; i++) {

System.out.println("异常时间:"+arr[1]+"错误信息:"+arr[2]+"异常类型:"+getArr(arr[0])[2]+"异常信息所在文件行数:"+getArr(arr[0])[1]+"日志文件名称:"+getArr(arr[0])[0]);

}

}

} catch (IOException e) {

e.printStackTrace();

} catch (JSchException e) {

e.printStackTrace();

} finally {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

channel.disconnect();

session.disconnect();

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值