当前位置:网站首页>获取并监控远程服务器日志
获取并监控远程服务器日志
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
- Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
- Yolov5 input (I) -- mosaic data enhancement | CSDN creative punch in
- Why should we rewrite hashcode when we rewrite the equals method?
- study hard and make progress every day
- Messy change of mouse style in win system
- Introduction to redis and explanation of data types
- [research materials] 2021 China's game industry brand report - Download attached
- Actual combat 8051 drives 8-bit nixie tube
- 1111 online map (30 points)
猜你喜欢
[research materials] 2021 China's game industry brand report - Download attached
2022-02-11 daily clock in: problem fine brush
Why is go language particularly popular in China
Unity tool Luban learning notes 1
穀歌 | 蛋白序列的深度嵌入和比對
Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
Basic knowledge of reflection (detailed explanation)
BIO、NIO、AIO区别
leetcode435. Non overlapping interval
随机推荐
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
Go language interface learning notes
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
【实战项目】自主web服务器
Go practice -- use JWT (JSON web token) in golang
Make your own dataset
Celebrate the new year together
Basic introduction of redis and explanation of eight types and transactions
appium1.22.x 版本後的 appium inspector 需單獨安裝
Gbase8s composite index (I)
(perfect solution) how to set the position of Matplotlib legend freely
BTC-密码学原理
Classification and discussion of plane grab detection methods based on learning
Go language interface learning notes Continued
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
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
Burp suite plug-in based on actual combat uses tips
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
Promise
Audio Focus Series: write a demo to understand audio focus and audiomananger