forked from elunez/eladmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
更改命令执行方式为shell,避免因为无法获得环境变量而无法启动程序的问题
- Loading branch information
Showing
4 changed files
with
66 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 41 additions & 59 deletions
100
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ExecuteShellUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,88 @@ | ||
package me.zhengjie.modules.mnt.util; | ||
|
||
import com.jcraft.jsch.*; | ||
import cn.hutool.core.io.IoUtil; | ||
import com.jcraft.jsch.ChannelShell; | ||
import com.jcraft.jsch.JSch; | ||
import com.jcraft.jsch.Session; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.*; | ||
import java.util.Vector; | ||
|
||
/** | ||
* 执行shell命令 | ||
* | ||
* @author: ZhangHouYing | ||
* @date: 2019/8/10 | ||
*/ | ||
@Slf4j | ||
public class ExecuteShellUtil { | ||
|
||
private String ipAddress; | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
public final int DEFAULT_SSH_PORT = 22; | ||
|
||
private Vector<String> stdout; | ||
|
||
public ExecuteShellUtil(final String ipAddress, final String username, final String password) { | ||
this.ipAddress = ipAddress; | ||
this.username = username; | ||
this.password = password; | ||
stdout = new Vector<String>(); | ||
} | ||
Session session; | ||
|
||
public int execute(final String command) { | ||
int returnCode = 0; | ||
JSch jsch = new JSch(); | ||
public ExecuteShellUtil(final String ipAddress, final String username, final String password) { | ||
try { | ||
Session session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT); | ||
JSch jsch = new JSch(); | ||
session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT); | ||
session.setPassword(password); | ||
session.setConfig("StrictHostKeyChecking", "no"); | ||
session.connect(30000); | ||
|
||
Channel channel = session.openChannel("exec"); | ||
((ChannelExec) channel).setCommand(command); | ||
session.connect(3000); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
channel.setInputStream(null); | ||
BufferedReader input = new BufferedReader(new InputStreamReader(channel.getInputStream())); | ||
} | ||
|
||
public int execute(final String command) { | ||
int returnCode = 0; | ||
ChannelShell channel = null; | ||
PrintWriter printWriter = null; | ||
BufferedReader input = null; | ||
stdout = new Vector<String>(); | ||
try { | ||
channel = (ChannelShell) session.openChannel("shell"); | ||
channel.connect(); | ||
log.info("The remote command is: " + command); | ||
|
||
input = new BufferedReader(new InputStreamReader(channel.getInputStream())); | ||
printWriter = new PrintWriter(channel.getOutputStream()); | ||
printWriter.println(command); | ||
printWriter.println("exit"); | ||
printWriter.flush(); | ||
log.info("The remote command is: "); | ||
String line; | ||
while ((line = input.readLine()) != null) { | ||
stdout.add(line); | ||
System.out.println(line); | ||
} | ||
input.close(); | ||
|
||
if (channel.isClosed()) { | ||
returnCode = channel.getExitStatus(); | ||
} | ||
channel.disconnect(); | ||
session.disconnect(); | ||
} catch (JSchException e) { | ||
e.printStackTrace(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return -1; | ||
}finally { | ||
IoUtil.close(printWriter); | ||
IoUtil.close(input); | ||
if (channel != null) { | ||
channel.disconnect(); | ||
} | ||
} | ||
return returnCode; | ||
} | ||
|
||
public boolean executeShell(String command) { | ||
int result = execute(command); | ||
for (String str : stdout) { | ||
log.info(str); | ||
} | ||
if (result == 0) { | ||
return true; | ||
} else { | ||
return false; | ||
public void close(){ | ||
if (session != null) { | ||
session.disconnect(); | ||
} | ||
} | ||
|
||
public String executeForResult(String command) { | ||
execute(command); | ||
StringBuilder sb = new StringBuilder(); | ||
for (String str : stdout) { | ||
sb.append(str); | ||
log.info(str); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* 返回值有三条就代表成功了,2条是没启动,多余三条代表脚本有问题 | ||
* @param command | ||
* @return | ||
*/ | ||
public int checkAppStatus(String command) { | ||
execute(command); | ||
for (String str : stdout) { | ||
log.info(str); | ||
} | ||
return stdout.size(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters