博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
远程shell脚本执行工具类
阅读量:7124 次
发布时间:2019-06-28

本文共 4408 字,大约阅读时间需要 14 分钟。

/**  * 远程shell脚本执行工具类  */ public class RemoteShellExecutorUtils {
private static final Logger logger = LoggerFactory.getLogger(RemoteShellExecutorUtils.class); private Connection conn; /** * 服务器IP */ private String ip; /** * 用户名 */ private String user; /** * 密码 */ private String password; private String charset = Charset.defaultCharset().toString(); private static final int TIME_OUT = 1000 * 5 * 60; /** * 构造函数 * * @param ip 服务器地址 * @param user 用户名 * @param pwd 密码 */ public RemoteShellExecutorUtils(String ip, String user, String pwd) {
this.ip = ip; this.user = user; this.password = pwd; } /** * 登录 * * @return * @throws IOException */ private boolean login() throws IOException {
conn = new Connection(ip); conn.connect(); return conn.authenticateWithPassword(user, password); } /** * 执行脚本 * * @param cmd shell命令 * @return */ public Map
exec(String cmd) {
InputStream stdOut = null; InputStream stdErr = null; String outStr = ""; String outErr = ""; Map
map = new HashMap
(); int ret = -1; try {
if (login()) {
// Open a new {@link Session} on this connection Session session = conn.openSession(); // Execute a command on the remote machine. session.execCommand(cmd); stdOut = new StreamGobbler(session.getStdout()); outStr = processStream(stdOut, charset); map.put("outStr",outStr); stdErr = new StreamGobbler(session.getStderr()); outErr = processStream(stdErr, charset); map.put("outErr",outErr); session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); System.out.println("outStr=" + outStr); System.out.println("outErr=" + outErr); ret = session.getExitStatus(); map.put("ret",ret+""); } else {
logger.error("登录远程机器失败:" + ip); } } catch (IOException e) {
e.printStackTrace(); } finally {
try {
if (conn != null) {
conn.close(); } if (stdOut != null) {
stdOut.close(); } if (stdErr != null) {
stdErr.close(); } } catch (IOException e) {
e.printStackTrace(); } } return map; } /** * @param in 输入流 * @param charset 字符编码 * @return * @throws Exception */ private String processStream(InputStream in, String charset) throws IOException {
byte[] buf = new byte[1024]; StringBuilder sb = new StringBuilder(); while (in.read(buf) != -1) {
sb.append(new String(buf, charset)); } return sb.toString(); } /** * 关闭连接 */ public void close(){
if(conn != null){
conn.close(); } } public static void main(String[] args) {
RemoteShellExecutorUtils executorUtils = new RemoteShellExecutorUtils("172.22.2.118", "hadoop", "hadoop"); String shell = String.format("sh %s %s %s %s", "/dataexchange/kafka/create.sh", "topic_create_test", "1", "2"); // String shell = String.format("sh %s %s", "/dataexchange/kafka/topics.sh","topic_create_test"); Map
map = executorUtils.exec(shell); if("".equals(map.get("outStr")) && "".equals(map.get("outErr")) && "0".equals(map.get("ret"))){
System.out.println("topic不存在"); } else if (!"".equals(map.get("outErr"))){
System.out.println("远程shell脚本执行异常>>>>"+map.get("outErr")); } else if (!"".equals(map.get("outStr"))){
System.out.println("topic已存在"); } } } 依赖包
org.jvnet.hudson
ganymed-ssh2
build210-hudson-1

转载于:https://www.cnblogs.com/zhoufly-blog/p/10101077.html

你可能感兴趣的文章
Ubuntu单系统(一):苦难深重的校园网
查看>>
MyBatis 学习笔记一基本对象
查看>>
jenkins pipeline slack
查看>>
CoverFlow效果控件无限循环效果
查看>>
Android Activity生命周期应用 网络设置
查看>>
jenkins + svn + mvn + tomcat搭建CI服务
查看>>
easyui 之 datagrid动态列与列宽自适应
查看>>
jvm运行时数据区域解析
查看>>
spring RestTemplate基本使用与总结
查看>>
【MongoDB 可视化工具Robomongo】下载与安装
查看>>
hadoop 问题
查看>>
android 动画
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
python基础二:之列表
查看>>
Koa 请求日志打点工具
查看>>
革命性新特性 | 单一应用跨多Kubernetes集群的部署与管理
查看>>
Linux LVM硬盘管理及LVM扩容
查看>>
Linux -数据库连接,且更改数据库密码
查看>>
一步步教你创建自己的数字货币(代币)进行ICO
查看>>