-
导入依赖 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>`spring-boot-starter-log4j2`</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.29.0</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.3</version>
</plugin>
</plugins>
</build>
-
测试代码 public static void main(String... args) throws IOException {
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect("10.2.176.239", 22);
ssh.authPassword("root", "Rzxlszy1Bpmcl");
Session session = null;
try {
session = ssh.startSession();
String execute = "grep CompositeLog /root/logs/system.log";
final Session.Command cmd = session.exec(execute);
InputStream inputStream = cmd.getInputStream();
OutputStream outputStream = IOUtils.readFully(inputStream);
String str = outputStream.toString();
System.out.println(str);
cmd.join(5, TimeUnit.SECONDS);
System.out.println("n** exit status: " + cmd.getExitStatus());
} finally {
try {
if (session != null) {
session.close();
}
} catch (IOException e) {
// 处理异常
}
ssh.disconnect();
}
}
-
问题讲述 spring-boot-starter-log4j2
这个日志依赖为什么要导入,因为连接Linux的依赖中有一个和这个依赖相关的API,如果不导入这个依赖会报错,错误如下: 这个错误的具体成因,我还不太清楚,有喜欢研究的可以自己研究一下。
|