当前位置:网站首页>获取并监控远程服务器日志
获取并监控远程服务器日志
2022-07-03 05:18:00 【焦虑的二狗】
测试JSch
原文地址:https://www.freesion.com/article/3557457395/
POM配置
<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency>
<dependency>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>maven-archetype</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
函数入口
import com.jcraft.jsch.JSchException;
import java.io.IOException;
public class Hellossh {
public static String host = "10.211.55.3";
public static String username = "parallels";
public static String password = "WW731298";
public static void main(String[] args) throws IOException, JSchException {
String command1 = "head -n 1 /home/admin/logs/*.log";
//执行输入信息
JSchUtil js = new JSchUtil(username, password, host, 22);
js.Connection();
js.executeCmd(command1);
}
}
UTIL类
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;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class JSchUtil {
//初始化类内变量
private int SSH_PORT;
private String username;
private String password;
private String hostip;
private String charset = "UTF-8";
private Session session;
public JSchUtil(String user, String password, String hostip, int port) {
this.username = user;
this.password = password;
this.hostip = hostip;
this.SSH_PORT = port;
}
/*
连接到指定IP
*/
public void Connection() throws JSchException {
JSch js = new JSch();
session = js.getSession(username, hostip, SSH_PORT);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(3000);
session.connect();
}
/*
关闭实例链接
*/
public void disconnection() {
if (session != null && session.isConnected()) {
session.disconnect();
}
}
/*
执行ssh命令接口
*/
public void executeCmd(String command) throws JSchException, IOException {
BufferedReader reader = null;
Channel channel = null;
System.out.println("**********************************");
System.out.println("InputCommand:" + "【" + command + "】");
System.out.println("**********************************");
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,
Charset.forName(charset)));
String buf = null;
System.out.println("OutPutResult:" + "\n");
StringBuffer buffer = new StringBuffer();
while ((buf = reader.readLine()) != null) {
System.out.println(buf);
buffer.append(buf);
buffer.append("\n");
}
System.out.println("**********************************");
//测试结果可返回做正则匹配进行逻辑判断
System.out.println("ReturnResult:" + "\n" + buffer.toString());
channel.disconnect();
}
}
边栏推荐
- appium1.22. Appium inspector after X version needs to be installed separately
- [set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
- 2022-02-12 daily clock in: problem fine brush
- Celebrate the new year together
- BTC-密码学原理
- ES7 easy mistakes in index creation
- Covering Safari and edge, almost all mainstream browsers have realized webgl 2.0 support
- 6.23星期四库作业
猜你喜欢
![[research materials] annual report of China's pension market in 2021 - Download attached](/img/24/622aeeb38de16ac84128b362ceeddb.jpg)
[research materials] annual report of China's pension market in 2021 - Download attached

谷歌 | 蛋白序列的深度嵌入和比对
![[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached](/img/01/052928e7f20ca671cdc4c30ae55258.jpg)
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
![[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)](/img/c2/87358af6b2b2892a6eceb751b3b60c.jpg)
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)

Without 50W bride price, my girlfriend was forcibly dragged away. What should I do

Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis

Principles of BTC cryptography
![[basic grammar] C language uses for loop to print Pentagram](/img/9e/021c6c0e748e0981d4233f74c83e76.jpg)
[basic grammar] C language uses for loop to print Pentagram

leetcode452. Detonate the balloon with the minimum number of arrows

BTC-密码学原理
随机推荐
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
聊聊如何利用p6spy进行sql监控
3dslam with 16 line lidar and octomap
Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
Explanation of several points needing attention in final (tested by the author)
Introduction to redis and explanation of data types
6.23星期四库作业
Kept hot standby and haproxy
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
Principles of BTC cryptography
1103 integer factorization (30 points)
Three representations of signed numbers: original code, inverse code and complement code
Intégration profonde et alignement des séquences de protéines Google
C language program ideas and several commonly used filters
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
ES7 easy mistakes in index creation
es7创建索引容易犯的错误
Differences among bio, NiO and AIO
Yolov5 network structure + code + application details | CSDN creation punch in
穀歌 | 蛋白序列的深度嵌入和比對