执行服务器中的指令
public class SSHUtil {
private static Logger logger = LoggerFactory.getLogger(SSHUtil.class);
private String ip;
private int port;
private String user;
private String pa;
private Session session ;
private ChannelExec channelExec;
private Session sessionServer ;
private ChannelExec channelExecServer;
public SSHUtil(String ip, int port, String user, String pa){
this.ip = ip;
this.port = port;
this.user = user;
this.pa = pa;
}
public void sessionConnect(){
JSch jsch = new JSch();
try {
session = jsch.getSession(user, ip, port);
// 跳过受检
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pa);
session.connect();
} catch (JSchException e) {
logger.error("SSH连接失败");
e.printStackTrace();
}
}
public void sessionConnectServer(){
JSch jsch = new JSch();
try {
sessionServer = jsch.getSession(user, ip, port);
// 跳过受检
sessionServer.setConfig("StrictHostKeyChecking", "no");
sessionServer.setPassword(pa);
sessionServer.connect();
} catch (JSchException e) {
logger.error("SSH连接失败");
e.printStackTrace();
}
}
public String exeCommand(String command){
sessionConnect();
try {
channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.setErrStream(System.err);
channelExec.connect();
logger.info("SSH远程命令"+command+"执行");
StringBuffer buffer = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line + "\n");
}
logger.info("SSH远程命令"+command+"执行完成");
return buffer.toString();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
channelExec.disconnect();
session.disconnect();
} catch (Exception e){
e.printStackTrace();
}
}
return null;
}
public HashMap<String, String> exeCommand1(String command){
sessionConnect();
HashMap<String, String> resultMap = new HashMap<>();
try {
channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.setErrStream(System.err);
channelExec.connect();
StringBuffer buffer = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(" ");
if(split.length > 1){
resultMap.put(split[0],split[1]);
}
buffer.append(line + "\n");
}
return resultMap;
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
channelExec.disconnect();
session.disconnect();
} catch (Exception e){
e.printStackTrace();
}
}
return null;
}
public List<String> exeCommand2(String command){
sessionConnect();
List<String> list = new ArrayList<>();
try {
channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.setErrStream(System.err);
channelExec.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
String line;
int i =1;
while ((line = br.readLine()) != null) {
if (i ==2) {
String[] split = line.split(" ");
for (String s : split) {
if (s != null && !s.equals("")) {
list.add(s);
}
}
}
i++;
}
return list;
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
channelExec.disconnect();
session.disconnect();
} catch (Exception e){
e.printStackTrace();
}
}
return null;
}
public List<String> exeCommandServer(String command){
sessionConnectServer();
List<String> list = new ArrayList<>();
try {
channelExecServer = (ChannelExec) sessionServer.openChannel("exec");
InputStream in = channelExecServer.getInputStream();
channelExecServer.setCommand(command);
channelExecServer.setErrStream(System.err);
channelExecServer.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
String line;
int i =1;
while ((line = br.readLine()) != null) {
if (i ==2) {
String[] split = line.split(" ");
for (String s : split) {
if (s != null && !s.equals("")) {
list.add(s);
}
}
}
i++;
}
return list;
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
channelExecServer.disconnect();
sessionServer.disconnect();
} catch (Exception e){
e.printStackTrace();
}
}
return null;
}
public void closeServer(){
if(channelExecServer != null){
channelExecServer.disconnect();
}
if(sessionServer != null){
sessionServer.disconnect();
}
}
}
多情自古空余恨
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构