当前位置:网站首页>獲取並監控遠程服務器日志
獲取並監控遠程服務器日志
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();
}
}
边栏推荐
- (完美解决)matplotlib图例(legend)如何自由设置其位置
- leetcode406. Rebuild the queue based on height
- ROS Compilation Principle
- 最大连续子段和(动态规划,递归,递推)
- 1099 build a binary search tree (30 points)
- 1115 counting nodes in a BST (30 points)
- Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
- Why is go language particularly popular in China
- Audio Focus Series: write a demo to understand audio focus and audiomananger
- Introduction to redis and explanation of data types
猜你喜欢
![[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)](/img/8b/c10423ee95200a0d94f9fb9dde76eb.jpg)
[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)

DEX net 2.0 for crawl detection

联想R7000显卡的拆卸与安装

3dslam with 16 line lidar and octomap

Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in

Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)

SimpleITK学习笔记

appium1.22.x 版本後的 appium inspector 需單獨安裝

Ueditor, FCKeditor, kindeditor editor vulnerability

Make your own dataset
随机推荐
cookie session jwt
编译GCC遇到的“pthread.h” not found问题
Chapter II program design of circular structure
Overview of basic knowledge of C language
联想R7000显卡的拆卸与安装
Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
Transferring images using flask
Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
6.23星期四库作业
Pessimistic lock and optimistic lock of multithreading
Basic knowledge of reflection (detailed explanation)
Redis 击穿穿透雪崩
Rust基础入门之(基本类型)
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
C language program ideas and several commonly used filters
Go practice - gorilla / handlers used by gorilla web Toolkit
Webapidom get page elements
2022-02-12 daily clock in: problem fine brush
Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
Basic introduction of redis and explanation of eight types and transactions