当前位置:网站首页>获取并监控远程服务器日志
获取并监控远程服务器日志
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();
}
}
边栏推荐
- Webapidom get page elements
- XML Configuration File
- [backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
- Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
- XML配置文件
- appium1.22.x 版本后的 appium inspector 需单独安装
- Shallow and first code
- Handler understands the record
- Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
- Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
猜你喜欢

Pessimistic lock and optimistic lock of multithreading

联想R7000显卡的拆卸与安装

音频焦点系列:手写一个demo理解音频焦点与AudioMananger

Gbase8s unique index and non unique index

穀歌 | 蛋白序列的深度嵌入和比對

小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计

leetcode406. Rebuild the queue based on height

Overview of basic knowledge of C language

Introduction to deep learning (II) -- univariate linear regression

Compile and decompile GCC common instructions
随机推荐
Introduction to deep learning - definition Introduction (I)
About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller
RT thread flow notes I startup, schedule, thread
Deep embedding and alignment of Google | protein sequences
Overview of basic knowledge of C language
Yolov5 input (II) | CSDN creative punch in
Unity tool Luban learning notes 1
Go practice - gorilla / handlers used by gorilla web Toolkit
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
Skip table: principle introduction, advantages and disadvantages of skiplist
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)
Interview question -- output the same characters in two character arrays
cookie session jwt
Redis Introduction et explication des types de données
My first Smartphone
1114 family property (25 points)
Introduction to deep learning (II) -- univariate linear regression
leetcode452. Detonate the balloon with the minimum number of arrows
leetcode860. Lemonade change
Make your own dataset