当前位置:网站首页>獲取並監控遠程服務器日志
獲取並監控遠程服務器日志
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();
}
}
边栏推荐
- Skip table: principle introduction, advantages and disadvantages of skiplist
- Transferring images using flask
- "250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
- Basic knowledge of reflection (detailed explanation)
- Introduction to rust Foundation (basic type)
- The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
- Introduction to redis and explanation of data types
- [set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
- (subplots用法)matplotlib如何绘制多个子图(轴域)
- Why should we rewrite hashcode when we rewrite the equals method?
猜你喜欢

Overview of basic knowledge of C language

The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article

Principles of BTC cryptography

XML Configuration File

SimpleITK学习笔记

2022-02-11 daily clock in: problem fine brush

Promise

Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
![[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)](/img/ce/d6f4fb30727e7436b6443537429ad4.png)
[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)

酒店公共广播背景音乐-基于互联网+的酒店IP网络广播系统设计
随机推荐
Robot capture experiment demonstration video
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
乾元通多卡聚合路由器的技术解析
How to connect the network: Chapter 1 CSDN creation punch in
Disassembly and installation of Lenovo r7000 graphics card
Redis 入門和數據類型講解
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
Gan network thought
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
ES7 easy mistakes in index creation
leetcode406. Rebuild the queue based on height
Introduction to rust Foundation (basic type)
Go language interface learning notes
1119 pre- and post order traversals (30 points)
112 stucked keyboard (20 points)
Why is go language particularly popular in China
Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
(完美解决)matplotlib图例(legend)如何自由设置其位置
[research materials] 2022q1 game preferred casual game distribution circular - Download attached