1、连接远程linux,执行命令
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
部分代码:
String cmd = "echo 'i love u' >/a.txt";
BufferedReader reader = null;
Channel channel = null;
Session session = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(username,host, 22);//22 默认端口
session.setConfig("PreferredAuthentications", "password");//固定配置,说明验证方法
session.setConfig("StrictHostKeyChecking", "no");//固定配置,减少验证
session.setPassword(password);
session.connect(3000); // making a connection with timeout.
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
getCurrentCall(map, currCallBeanList, CommUtil.stringNoNull(buf).trim());//数据处理
}
} catch (Exception e) {
log.error("查询ip:{}的当前通话出错。", GlobalPreConfig.ctiUrlList.get(j));
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
log.error("查询ip:{}的当前通话出错。", GlobalPreConfig.ctiUrlList.get(j));
e.printStackTrace();
}
channel.disconnect();
session.disconnect();
}
2、连接本机linux,执行命令
log.debug("查询本机ip:{}的当前通话开始", GlobalPreConfig.ctiUrlList.get(j));
boolean isLinux = System.getProperty("os.name").toLowerCase().contains("linux");
if (isLinux) {
BufferedReader br = SystemRun.batchRunForCurr(cmd, condMap.get(GlobalConstance.COMPANY_ID));
String lineStr = "";
String call_list = "";
while ((lineStr = br.readLine()) != null) {
call_list = CommUtil.stringNoNull(lineStr).trim();
getCurrentCall(condMap, currCallBeanList, call_list);
}
}
log.debug("查询本机ip:{}的当前通话结束", GlobalPreConfig.ctiUrlList.get(j));
/**
* 系统命令打包运行
*
* @param cmd
* @return
* @throws Exception
*/
private static BufferedReader batchRunForCurr(String cmd, Object companyId) throws Exception {
boolean isLinux = System.getProperty("os.name").toLowerCase().contains("linux");
if (isLinux) {
// first write sh file
String runFileName = "/root/tomcat9.0.31/bin/gw.sh";
String tomcatDir = System.getProperty("catalina.home");
if (!StringUtils.isEmpty(tomcatDir)) {
tomcatDir = tomcatDir.substring(tomcatDir.indexOf("/"));// linux
// tomcatDir=tomcatDir.substring(tomcat_win.indexOf("\\"));//windows
runFileName = tomcatDir + "/bin/gw.sh";
}
log.info("====>>>>>SH file:" + runFileName + " System batch run:" + cmd);
File file = new File(runFileName);
if (!file.exists()) {
PrintWriter pw = new PrintWriter(new FileWriter(runFileName));
pw.println(cmd);
pw.println("");
pw.close();
Process pr = Runtime.getRuntime().exec("chmod +x " + runFileName);
pr.waitFor();
}
// change mod to x
// run
Process process = Runtime.getRuntime().exec("sh " + runFileName);
process.waitFor();
InputStream inStreams = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inStreams));
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
outGobbler.start();
return br;
} else {
return null;
}
}
3、简洁方法:
// String res = JschUtil.exec(session, cmd, CharsetUtil.CHARSET_UTF_8);
// JschUtil.close(session);