重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍了通过Java实现bash命令过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
创新互联服务项目包括珠山网站建设、珠山网站制作、珠山网页制作以及珠山网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,珠山网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到珠山省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
1、BASH 命令简介
2、Java实现 BASH命令执行Shell脚本
1)代码实现如下:
import ch.ethz.ssh3.Connection; import ch.ethz.ssh3.Session; import ch.ethz.ssh3.StreamGobbler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class BashUtil { private Logger logger = LoggerFactory.getLogger(BashUtil.class); private String hostname; private String username; private String password; private int port; private Connection conn; private BashUtil() { } public BashUtil(String hostname, String username, String password) { this(hostname, username, password, 22); } public BashUtil(String hostname, String username, String password, Integer port) { this.hostname = hostname; this.username = username; this.password = password; if (port == null) { port = 22; } else { this.port = port; } } /** * 创建连接并认证 * @return */ public Boolean connection() { try { conn = new Connection(hostname, port); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); return isAuthenticated; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 关闭连接 */ public void close() { try { conn.close(); conn = null; } catch (Exception e) { e.printStackTrace(); } } /** * 执行shell命令 * @param command * @return */ public Listcommand(String command) { if (conn == null && !connection()) { logger.error("Authentication failed."); return null; } List result = new ArrayList (); try { Session sess = conn.openSession(); sess.execCommand(command); InputStream stdout = new StreamGobbler(sess.getStdout()); InputStream stderr = new StreamGobbler(sess.getStderr()); BufferedReader br_out = new BufferedReader(new InputStreamReader(stdout, "utf-8")); BufferedReader br_err = new BufferedReader(new InputStreamReader(stderr, "utf-8")); StringBuffer sb_err = new StringBuffer(); String line = null; while ((line = br_out.readLine()) != null) { result.add(line.trim()); } while ((line = br_err.readLine()) != null) { sb_err.append(line + "\n"); } if (isNotEmpty(sb_err.toString())) { logger.error(sb_err.toString()); return null; } return result; } catch (Exception e) { e.printStackTrace(); } return null; } private static boolean isEmpty(String content) { if (content == null) { return true; } else { return "".equals(content.trim()) || "null".equalsIgnoreCase(content.trim()); } } private static boolean isNotEmpty(String content) { return !isEmpty(content); } public static void main(String[] args){ String hostname = "192.168.123.234"; // 此处根据实际情况,换成自己需要访问的主机IP String userName = "root"; String password = "password"; Integer port = 22; String command = "cd /home/miracle&&pwd&&ls&&cat luna.txt"; BashUtil bashUtil = new BashUtil(hostname, userName, password, port); List resultList = bashUtil.command(command); StringBuffer result = new StringBuffer(""); resultList.forEach(str -> result.append(str + "\n")); System.out.println("执行的结果如下: \n" + result.toString()); } }
2)执行结果如下:
执行的结果如下: /home/miracle luna.txt Hello, I'm SshUtil. Nice to meet you.^_^
3)pom.xml引用依赖包如下:
org.slf4j slf4j-api 1.7.21 ch.ethz.ganymed ganymed-ssh3 262
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。