java远程读取linux文件

所需依赖包

  <!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
  <dependency>
   <groupId>ch.ethz.ganymed</groupId>
   <artifactId>ganymed-ssh2</artifactId>
   <version>262</version>
  </dependency>


java代码


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class TestRemoteConnect {
 public static void main(String[] args) {
  String path = "/home/lyf/apps/test/info";
  String hostName = "192.168.1.106";
  int port = 22;
  String username = "root";
  String password = "123456";
  Connection ss = getConnect(hostName, username, password, port);
  if (fileExist(path, ss)) {
   readLogFile(path, ss);
  }
 }
 public static Connection getConnect(String hostName, String username, String password, int port) {
  Connection conn = new Connection(hostName, port);
  try {
   // 连接到主机
   conn.connect();
   // 使用用户名和密码校验
   boolean isconn = conn.authenticateWithPassword(username, password);
   if (!isconn) {
    System.out.println("用户名称或者是密码不正确");
   } else {
    System.out.println("服务器连接成功.");
    return conn;
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
 }
 public static boolean fileExist(String path, Connection conn) {
  if (conn != null) {
   Session ss = null;
   try {
    ss = conn.openSession();
    ss.execCommand("ls -l ".concat(path));
    InputStream is = new StreamGobbler(ss.getStdout());
    BufferedReader brs = new BufferedReader(new InputStreamReader(is));
    String line = "";
    while (true) {
     String lineInfo = brs.readLine();
     ;
     if (lineInfo != null) {
      line = line + lineInfo;
     } else {
      break;
     }
    }
    brs.close();
    if (line != null && line.length() > 0 && line.startsWith("-")) {
     return true;
    }
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    // 连接的Session和Connection对象都需要关闭
    if (ss != null) {
     ss.close();
    }
   }
  }
  return false;
 }
 public static void readLogFile(String path, Connection conn) {
  if (conn != null) {
   Session ss = null;
   try {
    ss = conn.openSession();
    ss.execCommand("tail -100 ".concat(path));
    InputStream is = new StreamGobbler(ss.getStdout());
    BufferedReader brs = new BufferedReader(new InputStreamReader(is));
    while (true) {
     String line = brs.readLine();
     if (line == null) {
      break;
     } else {
      System.out.println(line);
     }
    }
    brs.close();
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    // 连接的Session和Connection对象都需要关闭
    if (ss != null) {
     ss.close();
    }
    if (conn != null) {
     conn.close();
    }
   }
  }
 }
}




  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值