|
|
@@ -150,28 +150,60 @@ public class Sftp {
|
|
|
channel.setCommand(command);
|
|
|
channel.setInputStream(null);
|
|
|
channel.setErrStream(System.err);
|
|
|
+ InputStream input = channel.getInputStream();
|
|
|
+ InputStream error = channel.getErrStream();
|
|
|
+
|
|
|
// timeout 设置为 10 分钟
|
|
|
channel.connect(600_000);
|
|
|
- InputStream input = channel.getInputStream();
|
|
|
- try {
|
|
|
- BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
|
|
|
- String line;
|
|
|
- while((line = inputReader.readLine()) != null) {
|
|
|
- sb.append(line).append(System.lineSeparator());
|
|
|
+ /*byte[] tmp = new byte[1024];
|
|
|
+ int i = 0;
|
|
|
+ while (true) {
|
|
|
+ while (input.available() > 0) {
|
|
|
+ i = input.read(tmp, 0, 1024);
|
|
|
+ if (i < 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- } finally {
|
|
|
- if (input != null) {
|
|
|
- try {
|
|
|
- input.close();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+
|
|
|
+ if (channel.isClosed()) {
|
|
|
+ if (input.available() > 0) {
|
|
|
+ continue;
|
|
|
}
|
|
|
+
|
|
|
+ log.info("channel closed with status: {}", channel.getExitStatus());
|
|
|
+ break;
|
|
|
}
|
|
|
+
|
|
|
+ try {
|
|
|
+ Thread.sleep(1000);
|
|
|
+ } catch(Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append(new String(tmp, 0, i));
|
|
|
+ */
|
|
|
+ // stdin
|
|
|
+ BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
|
|
|
+ String line;
|
|
|
+ while((line = inputReader.readLine()) != null) {
|
|
|
+ sb.append(line).append(System.lineSeparator());
|
|
|
+ }
|
|
|
+
|
|
|
+ // stderr
|
|
|
+ StringBuilder sb1 = new StringBuilder();
|
|
|
+ BufferedReader errorReader = new BufferedReader(new InputStreamReader(error));
|
|
|
+ String line1;
|
|
|
+ while((line1 = errorReader.readLine()) != null) {
|
|
|
+ sb1.append(line1).append(System.lineSeparator());
|
|
|
}
|
|
|
|
|
|
int statusCode = channel.getExitStatus();
|
|
|
ShellResult shellResult = new ShellResult(statusCode);
|
|
|
- shellResult.setResult(sb.toString());
|
|
|
+ if (statusCode == 0) {
|
|
|
+ shellResult.setResult(sb.toString());
|
|
|
+ } else {
|
|
|
+ shellResult.setResult(sb1.toString());
|
|
|
+ }
|
|
|
|
|
|
if (channel.isConnected()) {
|
|
|
channel.disconnect();
|
|
|
@@ -212,7 +244,7 @@ public class Sftp {
|
|
|
String command = String.format("cd %s && %s shutdown.sh", remoteDir, bash);
|
|
|
ShellResult shellResult = exec(session, command);
|
|
|
if (!shellResult.isSuccess()) {
|
|
|
- log.info("shutdown application failed\nexitCode {}\nmsg:{}", shellResult.getExitCode(), shellResult.getResult());
|
|
|
+ log.info("shutdown application failed\nexitCode: {}\nresult:\n{}", shellResult.getExitCode(), shellResult.getResult());
|
|
|
System.exit(-1);
|
|
|
} else {
|
|
|
log.info("shutdown application successfully");
|
|
|
@@ -227,7 +259,7 @@ public class Sftp {
|
|
|
String command = String.format("cd %s && %s start.sh", remoteDir, bash);
|
|
|
ShellResult shellResult = exec(session, command);
|
|
|
if (!shellResult.isSuccess()) {
|
|
|
- log.info("start application failed\nexitCode {}\nmsg:{}", shellResult.getExitCode(), shellResult.getResult());
|
|
|
+ log.info("start application failed\nexitCode: {}\nresult:\n{}", shellResult.getExitCode(), shellResult.getResult());
|
|
|
System.exit(-1);
|
|
|
} else {
|
|
|
log.info("start application successfully");
|
|
|
@@ -237,4 +269,26 @@ public class Sftp {
|
|
|
session.disconnect();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void exec(RemoteHost remoteHost, String command) throws Exception {
|
|
|
+ Session session = getSession(remoteHost);
|
|
|
+ ShellResult shellResult = exec(session, command);
|
|
|
+ log.info("\nexitCode: {}\nresult:\n{}", shellResult.getExitCode(), shellResult.getResult());
|
|
|
+
|
|
|
+ if (session.isConnected()) {
|
|
|
+ session.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String host = "192.168.0.180";
|
|
|
+ int port = 22;
|
|
|
+ String username = "root";
|
|
|
+ String password = "gsh";
|
|
|
+ RemoteHost remoteHost = new RemoteHost(host, port, username, password, null);
|
|
|
+
|
|
|
+ String command = "docker ps -a";
|
|
|
+ Sftp sftp = new Sftp();
|
|
|
+ sftp.exec(remoteHost, command);
|
|
|
+ }
|
|
|
}
|