远程shell脚本执行工具类
/**
* 远程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<String,String> exec(String cmd) {
InputStream stdOut = null;
InputStream stdErr = null;
String outStr = "";
String outErr = "";
Map<String,String> map = new HashMap<String,String>();
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<String,String> 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已存在");
}
}
} 依赖包
<dependency>
<groupId>org.jvnet.hudson</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210-hudson-1</version>
</dependency>
远程shell脚本执行工具类的更多相关文章
- 【原】Java程序调用远程Shell脚本
此程序的目的是执行远程机器上的Shell脚本. [环境参数]远程机器IP:192.168.234.123用户名:root密码:rootShell脚本的路径:/home/IFileGenTool/Bak ...
- windows下建立文件的换行符^M导致linux下的shell脚本执行错误的解决方式
常常在windows下编辑的文件远程传送到linux下的时候每行末尾都会出现^M.这将导致shell脚本执行错误,主要是由于dos下的编辑器和linux下的编辑器对文件末行的回车符处理不一致导致. 主 ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
- Linux之Shell 脚本加密工具-shc
Much effort, much prosperity. 为什么要加密Shell脚本呢?当然是为了安全! 可能脚本里面涉及到密码之类的就需要进行加密了 一.下载安装shc工具 要保护自己编写的she ...
- linux运维自动化shell脚本小工具
linux运维shell 脚本小工具,如要分享此文章,请注明文章出处,以下脚本仅供参考,若放置在服务器上出错,后果请自负 1.检测cpu剩余百分比 #!/bin/bash #Inspect CPU # ...
- shell脚本执行错误 $'\r':command not found
shell脚本执行错误 $'\r':command not found Linux下有命令dos2unix 可以用一下命令测试 vi -b filename 我们只要输入dos2unix *.sh就可 ...
- 2.8 补充:shell脚本执行方法
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本 ...
- linux下shell脚本执行jar文件
最近在搞一个shell脚本启动jar文件个关闭jar文件的东东.搞得我都蛋疼了.今天晚上终于弄好了 话说,小弟的linux只是刚入门,经过各方查资料终于搞定了.话不多说,下面开始上小弟写的shell脚 ...
- shell脚本 awk工具
awk工具概述awk编程语言/数据处理引擎基于模式匹配检查输入文本,逐行处理并输出通常在shell脚本中,或取指定的数据单独用时,可对文本数据做统计 命令格式格式一:awk [选项] '[条件]{编辑 ...
随机推荐
- 找到多个与名为“Home”的控制器匹配的类型。解决方法
“/”应用程序中的服务器错误. 找到多个与名为“Home”的控制器匹配的类型.如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配 ...
- ABP架构学习系列四:集成Dapper
之前,一直想集成Dapper到项目中,但是一直没成功,今天把abp升级到最新版,然后按教程来,就可以了,呵呵 现在,基于上一篇的源码进行升级和集成dapper,将abp升级到3.8.2 官方 ...
- gitlab 随笔
输入: $ git init $ git add . $ git commit -m 'init commit' 绿色部分为gitlab网页的项目创建后下面的ssh路径,也可以通过查看gitlab网页 ...
- VUE 父组件与子组件交互
1. 概述 1.1 说明 在项目过程中,会有很多重复功能在多个页面中处理,此时则需要把这些重复的功能进行单独拎出,编写公用组件(控件)进行引用.在VUE中,组件是可复用的VUE实例,此时组件中的dat ...
- luoguP1941-
飞扬的小鸟 20分&50分: #include<iostream> #include<cstdio> #include<cstring> #include& ...
- 洛谷 P1983 车站分级
题目链接 https://www.luogu.org/problemnew/show/P1983 题目描述 一条单向的铁路线上,依次有编号为 1,2,…,n的 n个火车站.每个火车站都有一个级别,最低 ...
- centos7手把手教你搭建zabbix监控
Centos7安装部署zabbix3.4 centos系统版本: 1.安装前需要先关闭selinux和firewall. 1.1 [root@zabbix ~]# vi /etc/selinux/co ...
- kubernetes集群网络配置方案
1. 直接路由 在每个节点上添加其他节点的静态路由,就可以将不同物理机的docker0网桥互联互通. 我的两个节点的IP分别为:172.16.252.218和172.16.252.209. 两个doc ...
- IDEA下运行 mybatis报错 Parameter 'arg0' not found. Available parameters are [autoRecharge, id, param1, param2]
电脑换系统之后重新安装一了 一下idea 项目运行时出现了以下错误, [autoRecharge, id, param1, param2] 或 [arg0, id, arg1, param2] 参考地 ...
- ubuntu日常使用指南
目录 换源 开发相关的基本包 vimrc python, pip zsh, oh-my-zsh, josh 配置android相关环境 查看库文件(libxxx.a/libxxx.so,动态静态库均可 ...