Merge pull request 'При запуске идёт подключение ко всем серверам' (#2) from feature/add_ssh_connect into master
Reviewed-on: L_DelOff/ServerMonitorBot#2feature/add-status-button
commit
91efa22e12
5
pom.xml
5
pom.xml
|
@ -42,6 +42,11 @@
|
|||
<artifactId>telegrambots</artifactId>
|
||||
<version>6.7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcraft</groupId>
|
||||
<artifactId>jsch</artifactId>
|
||||
<version>0.1.55</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
|
|
@ -2,8 +2,11 @@ package ru.ldeloff.servermonitorbot;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import ru.ldeloff.servermonitorbot.config.SshConfig;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(SshConfig.class)
|
||||
public class ServerMonitorBotApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package ru.ldeloff.servermonitorbot.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "ssh")
|
||||
public class SshConfig {
|
||||
private List<SshServer> servers;
|
||||
|
||||
public List<SshServer> getServers() {
|
||||
return servers;
|
||||
}
|
||||
|
||||
public void setServers(List<SshServer> servers) {
|
||||
this.servers = servers;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.service.SshService;
|
||||
|
||||
@Component
|
||||
public class SshConnect implements ApplicationRunner {
|
||||
final SshService sshService;
|
||||
|
||||
public SshConnect(SshService sshService) {
|
||||
this.sshService = sshService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
sshService.ConnectToAllServer();
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot;
|
||||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
|
@ -0,0 +1,15 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@Component
|
||||
public class SshServer {
|
||||
private String host;
|
||||
private int port;
|
||||
private String user;
|
||||
private String password;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
|
||||
public interface SshService {
|
||||
void ConnectToAllServer();
|
||||
Session ConnectToServer(SshServer sshServer) throws JSchException;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ldeloff.servermonitorbot.config.SshConfig;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SshServiceImpl implements SshService{
|
||||
|
||||
private final List<SshServer> sshServers;
|
||||
|
||||
@Autowired
|
||||
public SshServiceImpl(SshConfig sshConfig) {
|
||||
this.sshServers = sshConfig.getServers();
|
||||
}
|
||||
|
||||
private List<Session> sessions = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void ConnectToAllServer() {
|
||||
sshServers.forEach(sshServer -> {
|
||||
try {
|
||||
Session session = ConnectToServer(sshServer);
|
||||
log.info("Успешно подключён к " + sshServer.getHost());
|
||||
sessions.add(session);
|
||||
} catch (JSchException e) {
|
||||
log.warn("Не удалось соединиться с " + sshServer.getHost() + ": " + e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Session ConnectToServer(SshServer sshServer) throws JSchException {
|
||||
Session session = new JSch().getSession(sshServer.getUser(),
|
||||
sshServer.getHost(),
|
||||
sshServer.getPort());
|
||||
session.setPassword(sshServer.getPassword());
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
session.connect();
|
||||
return session;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,13 @@
|
|||
bot:
|
||||
name: "ServerMonitorBot"
|
||||
token: "token"
|
||||
ssh:
|
||||
servers:
|
||||
-
|
||||
host: "url"
|
||||
port: 22
|
||||
user: "user"
|
||||
password: "pass"
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue