Skip to content

[UPDATE] Fix execute command, Fix Sftp auto close #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/main/java/com/yann/ssh/pool/SshSessionHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ public void keepAlive() throws Exception {
}
}

public SshResponse execCommand(String command) {
return this.execCommand(command, -1, logger);
public SshResponse execCommand(String command, boolean usePty) {
return this.execCommand(command, -1, logger, usePty);
}

public SshResponse execCommand(String command, long timeout) {
return this.execCommand(command, timeout, logger);
public SshResponse execCommand(String command, long timeout, boolean usePty) {
return this.execCommand(command, timeout, logger, usePty);
}

public SshResponse execCommand(String command, long timeout, Logger logger) {
return this.execCommand(createChannelExec(), command, timeout, logger);
public SshResponse execCommand(String command, long timeout, Logger logger, boolean usePty) {
return this.execCommand(createChannelExec(), command, timeout, logger, usePty);
}

/**
Expand All @@ -129,13 +129,13 @@ public SshResponse execCommand(String command, long timeout, Logger logger) {
* @param customLogger logger for custom
* @return ssh response
*/
public SshResponse execCommand(ChannelExec channelExec, String command, long timeout, Logger customLogger) {
public SshResponse execCommand(ChannelExec channelExec, String command, long timeout, Logger customLogger, boolean usePty) {
customLogger.info("Executing command {} on session:{}", command, this);
channelExec.setCommand(command);
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
// it will kill process when channel disconnect, default true
channelExec.setPty(true);
channelExec.setPty(usePty);

SshResponse response = new SshResponse();
try (
Expand Down Expand Up @@ -279,6 +279,8 @@ public boolean sftpDir(ChannelSftp channelSftp, String localDirPath, String remo
} catch (Exception e) {
throw new SshException(
"sftp " + localDirPath + " to " + sshHost.toString() + ":" + remoteDirPath + " failed.", e);
} finally {
close(channelSftp);
}
}

Expand All @@ -290,15 +292,15 @@ public boolean sftpDir(ChannelSftp channelSftp, String localDirPath, String remo
*/
public boolean createDirOnRemote(String remoteDirPath) {
logger.info("create directory:{} on remote:{}", remoteDirPath, sshHost.toString());
SshResponse response = execCommand("mkdir -p " + remoteDirPath);
SshResponse response = execCommand("mkdir -p " + remoteDirPath, true);
return response.getExitCode() == 0;
}

public void clearPath(String path) {
if (SINGLE_SLASH.equals(path)) {
return;
}
execCommand("rm -rf " + path);
execCommand("rm -rf " + path, true);
}

private void upload(ChannelSftp channelSftp, String src, String dst, boolean enableUploadMonitor,
Expand Down