当前位置:网站首页>獲取並監控遠程服務器日志
獲取並監控遠程服務器日志
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();
}
}
边栏推荐
- Based on RFC 3986 (unified resource descriptor (URI): general syntax)
- 3dslam with 16 line lidar and octomap
- DEX net 2.0 for crawl detection
- Yolov5 network structure + code + application details | CSDN creation punch in
- 【实战项目】自主web服务器
- Gan network thought
- Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
- 谷歌 | 蛋白序列的深度嵌入和比对
- Introduction to redis and explanation of data types
- [research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
猜你喜欢

Pessimistic lock and optimistic lock of multithreading

JS scope

Burp suite plug-in based on actual combat uses tips

联想R7000显卡的拆卸与安装

大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南

Pan details of deep learning

Disassembly and installation of Lenovo r7000 graphics card

Yolov5 model construction source code details | CSDN creation punch in

Online VR model display - 3D visual display solution

Progressive multi grasp detection using grasp path for rgbd images
随机推荐
Messy change of mouse style in win system
Common methods of JS array
The IntelliJ platform completely disables the log4j component
JS function algorithm interview case
Disassembly and installation of Lenovo r7000 graphics card
leetcode860. Lemonade change
谷歌 | 蛋白序列的深度嵌入和比对
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
Gbase8s unique index and non unique index
乾元通多卡聚合路由器的技术解析
Technical analysis of qianyuantong multi card aggregation router
1107 social clusters (30 points)
Go practice -- gorilla / websocket used by gorilla web Toolkit
Go practice -- use redis in golang (redis and go redis / redis)
Go practice -- design patterns in golang's singleton
cookie session jwt
Redis 入门和数据类型讲解
[practical project] autonomous web server
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
Go language interface learning notes Continued