连接服务器使用用户设置的端口,提供测试连接按钮,测试配置信息是否正确

pull/214/head
zhanghouying 2019-11-27 16:39:32 +08:00
parent 53583b203c
commit 175a2eb686
5 changed files with 36 additions and 6 deletions

View File

@ -61,4 +61,13 @@ public class ServerDeployController {
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);
}
}

View File

@ -58,4 +58,11 @@ public interface ServerDeployService {
* @return /
*/
ServerDeployDto findByIp(String ip);
/**
*
* @param resources
* @return
*/
Boolean testConnect(ServerDeploy resources);
}

View File

@ -379,7 +379,7 @@ public class DeployServiceImpl implements DeployService {
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) {

View File

@ -6,6 +6,7 @@ import me.zhengjie.modules.mnt.service.ServerDeployService;
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,6 +57,21 @@ public class ServerDeployServiceImpl implements ServerDeployService {
return serverDeployMapper.toDto(deploy);
}
@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) {

View File

@ -18,16 +18,14 @@ import java.util.Vector;
@Slf4j
public class ExecuteShellUtil {
public final int DEFAULT_SSH_PORT = 22;
private Vector<String> 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);