From 175a2eb686c875fe9cf4991e1a2feaf5e3277970 Mon Sep 17 00:00:00 2001 From: zhanghouying Date: Wed, 27 Nov 2019 16:39:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=94=A8=E6=88=B7=E8=AE=BE=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=EF=BC=8C=E6=8F=90=E4=BE=9B=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=8C=89=E9=92=AE=EF=BC=8C=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=A1=E6=81=AF=E6=98=AF=E5=90=A6=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mnt/rest/ServerDeployController.java | 9 +++++++++ .../mnt/service/ServerDeployService.java | 7 +++++++ .../mnt/service/impl/DeployServiceImpl.java | 2 +- .../service/impl/ServerDeployServiceImpl.java | 18 +++++++++++++++++- .../modules/mnt/util/ExecuteShellUtil.java | 6 ++---- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/ServerDeployController.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/ServerDeployController.java index 2efd69e70..0ae6ad90d 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/ServerDeployController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/ServerDeployController.java @@ -61,4 +61,13 @@ public ResponseEntity delete(@PathVariable Long id){ serverDeployService.delete(id); return new ResponseEntity(HttpStatus.OK); } + + @Log("测试连接服务器") + @ApiOperation(value = "测试连接服务器") + @PostMapping("/testConnect") + @PreAuthorize("@el.check('serverDeploy:add')") + public ResponseEntity testConnect(@Validated @RequestBody ServerDeploy resources){ + return new ResponseEntity<>(serverDeployService.testConnect(resources),HttpStatus.CREATED); + } + } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/ServerDeployService.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/ServerDeployService.java index 851e4d1ff..6c98af1bd 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/ServerDeployService.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/ServerDeployService.java @@ -58,4 +58,11 @@ public interface ServerDeployService { * @return / */ ServerDeployDto findByIp(String ip); + + /** + * 测试登录服务器 + * @param resources + * @return + */ + Boolean testConnect(ServerDeploy resources); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java index 513712d5f..f85f66782 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java @@ -379,7 +379,7 @@ private ExecuteShellUtil getExecuteShellUtil(String ip) { sendMsg("IP对应服务器信息不存在:" + ip, MsgType.ERROR); throw new BadRequestException("IP对应服务器信息不存在:" + ip); } - return new ExecuteShellUtil(ip, serverDeployDTO.getAccount(), serverDeployDTO.getPassword()); + return new ExecuteShellUtil(ip, serverDeployDTO.getAccount(), serverDeployDTO.getPassword(),serverDeployDTO.getPort()); } private ScpClientUtil getScpClientUtil(String ip) { diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/ServerDeployServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/ServerDeployServiceImpl.java index 542e12571..115f3a4a1 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/ServerDeployServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/ServerDeployServiceImpl.java @@ -6,6 +6,7 @@ import me.zhengjie.modules.mnt.service.dto.ServerDeployDto; import me.zhengjie.modules.mnt.service.dto.ServerDeployQueryCriteria; import me.zhengjie.modules.mnt.service.mapper.ServerDeployMapper; +import me.zhengjie.modules.mnt.util.ExecuteShellUtil; import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.QueryHelp; import me.zhengjie.utils.ValidationUtil; @@ -56,7 +57,22 @@ public ServerDeployDto findByIp(String ip) { return serverDeployMapper.toDto(deploy); } - @Override + @Override + public Boolean testConnect(ServerDeploy resources) { + ExecuteShellUtil executeShellUtil = null; + try { + executeShellUtil = new ExecuteShellUtil(resources.getIp(), resources.getAccount(), resources.getPassword(),resources.getPort()); + return executeShellUtil.execute("ls")==0; + } catch (Exception e) { + return false; + }finally { + if (executeShellUtil != null) { + executeShellUtil.close(); + } + } + } + + @Override @Transactional(rollbackFor = Exception.class) public ServerDeployDto create(ServerDeploy resources) { return serverDeployMapper.toDto(serverDeployRepository.save(resources)); diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ExecuteShellUtil.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ExecuteShellUtil.java index e2b799558..708c24305 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ExecuteShellUtil.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ExecuteShellUtil.java @@ -18,16 +18,14 @@ @Slf4j public class ExecuteShellUtil { - public final int DEFAULT_SSH_PORT = 22; - private Vector stdout; Session session; - public ExecuteShellUtil(final String ipAddress, final String username, final String password) { + public ExecuteShellUtil(final String ipAddress, final String username, final String password,int port) { try { JSch jsch = new JSch(); - session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT); + session = jsch.getSession(username, ipAddress, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(3000);