Skip to content

Commit

Permalink
连接服务器使用用户设置的端口,提供测试连接按钮,测试配置信息是否正确
Browse files Browse the repository at this point in the history
  • Loading branch information
zhy6599 committed Nov 27, 2019
1 parent 53583b2 commit 175a2eb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ public interface ServerDeployService {
* @return /
*/
ServerDeployDto findByIp(String ip);

/**
* 测试登录服务器
* @param resources
* @return
*/
Boolean testConnect(ServerDeploy resources);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
@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);
Expand Down

0 comments on commit 175a2eb

Please sign in to comment.