Java远程实现Linux文件内容读取.
来源:互联网 发布:spss软件免费下载 编辑:程序博客网 时间:2024/05/22 08:23
//首先要下载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<LoggerForm> list = new ArrayList<LoggerForm>(); //返回数据 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 0
- Java远程实现Linux文件内容读取.
- JAVA实现远程文件读取!
- JAVA实现远程文件读取!
- JAVA实现远程文件读取
- java实现远程文件的读取
- java 读取远程文件 实现继传。。。
- java实现读取指定文件内容
- java实现读取txt文件中的内容
- linux读取文件内容
- java读取远程文件
- java读取远程文件
- Java读取远程文件
- java读取远程文件
- java读取远程文件
- Java读取文件内容
- JAVA读取文件内容
- Java读取文件内容
- java读取文件内容
- VS2015编写C语言程序连接mysql并插入变量
- HDU 5977 Garden of Eden(树分治)
- android驱动一些外设调试记录
- mysql 随机获取记录
- 我所理解的Java NIO
- Java远程实现Linux文件内容读取.
- java浮点型分析
- spacemacs删除行尾无意义的空格符号
- 网络编程3之TCP/IP协议
- 【转载】深入解析 ES6:箭头函数
- android 富文本编辑框/android XRichText删除图片
- 冒泡排序SDUT2015期末
- 系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式
- mysql常用技能分享