当前位置:网站首页>獲取並監控遠程服務器日志
獲取並監控遠程服務器日志
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();
}
}
边栏推荐
- JS string and array methods
- Introduction to webrtc protocol -- an article to understand dtls, SRTP, srtcp
- Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
- 1114 family property (25 points)
- Gbase8s unique index and non unique index
- Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
- 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
- Deep embedding and alignment of Google | protein sequences
- Go language interface learning notes Continued
- Online VR model display - 3D visual display solution
猜你喜欢

Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)

Make your own dataset

JS dynamic table creation
![[research materials] annual report of China's pension market in 2021 - Download attached](/img/24/622aeeb38de16ac84128b362ceeddb.jpg)
[research materials] annual report of China's pension market in 2021 - Download attached

Ueditor, FCKeditor, kindeditor editor vulnerability

appium1.22.x 版本后的 appium inspector 需单独安装
![[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)

leetcode452. Detonate the balloon with the minimum number of arrows

Compile and decompile GCC common instructions

How to connect the network: Chapter 1 CSDN creation punch in
随机推荐
1107 social clusters (30 points)
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
Go language interface learning notes Continued
1094 the largest generation (25 points)
1095 cars on campus (30 points)
XML配置文件
[basic grammar] C language uses for loop to print Pentagram
3dslam with 16 line lidar and octomap
Ueditor, FCKeditor, kindeditor editor vulnerability
1086 tree traversals again (25 points)
1087 all roads lead to Rome (30 points)
(perfect solution) how to set the position of Matplotlib legend freely
Make your own dataset
2022-02-12 daily clock in: problem fine brush
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
Intégration profonde et alignement des séquences de protéines Google
leetcode406. Rebuild the queue based on height
es7创建索引容易犯的错误
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
Chapter II program design of circular structure