java远程linux的shell(精选)
来源:互联网 发布:日本人中国知乎 编辑:程序博客网 时间:2024/06/04 01:23
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import org.apache.commons.lang.StringUtils; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; /** * 远程执行linux的shell script * @author Ickes * @since V0.1 */ public class RemoteExecuteCommand { //字符编码默认是utf-8 private static String DEFAULTCHART="UTF-8"; private Connection conn; private String ip; private String userName; private String userPwd; public RemoteExecuteCommand(String ip, String userName, String userPwd) { this.ip = ip; this.userName = userName; this.userPwd = userPwd; } public RemoteExecuteCommand() { } /** * 远程登录linux的主机 * @author Ickes * @since V0.1 * @return * 登录成功返回true,否则返回false */ public Boolean login(){ boolean flg=false; try { conn = new Connection(ip); conn.connect();//连接 flg=conn.authenticateWithPassword(userName, userPwd);//认证 } catch (IOException e) { e.printStackTrace(); } return flg; } /** * @author Ickes * 远程执行shll脚本或者命令 * @param cmd * 即将执行的命令 * @return * 命令执行完后返回的结果值 * @since V0.1 */ public String execute(String cmd){ String result=""; try { if(login()){ Session session= conn.openSession();//打开一个会话 session.execCommand(cmd);//执行命令 result=processStdout(session.getStdout(),DEFAULTCHART); //如果为得到标准输出为空,说明脚本执行出错了 if(StringUtils.isBlank(result)){ result=processStdout(session.getStderr(),DEFAULTCHART); } conn.close(); session.close(); } } catch (IOException e) { e.printStackTrace(); } return result; } /** * @author Ickes * 远程执行shll脚本或者命令 * @param cmd * 即将执行的命令 * @return * 命令执行成功后返回的结果值,如果命令执行失败,返回空字符串,不是null * @since V0.1 */ public String executeSuccess(String cmd){ String result=""; try { if(login()){ Session session= conn.openSession();//打开一个会话 session.execCommand(cmd);//执行命令 result=processStdout(session.getStdout(),DEFAULTCHART); conn.close(); session.close(); } } catch (IOException e) { e.printStackTrace(); } return result; } /** * 解析脚本执行返回的结果集 * @author Ickes * @param in 输入流对象 * @param charset 编码 * @since V0.1 * @return * 以纯文本的格式返回 */ private String processStdout(InputStream in, String charset){ InputStream stdout = new StreamGobbler(in); StringBuffer buffer = new StringBuffer();; try { BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset)); String line=null; while((line=br.readLine()) != null){ buffer.append(line+"\n"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer.toString(); } public static void setCharset(String charset) { DEFAULTCHART = charset; } public Connection getConn() { return conn; } public void setConn(Connection conn) { this.conn = conn; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } }
阅读全文
0 0
- java远程linux的shell(精选)
- 精选37条强大的常用linux shell命令组合
- 精选37条强大的常用linux shell命令组合
- 精选37条强大的常用linux shell命令组合
- 精选37条强大的常用linux shell命令组合
- 精选37条强大的常用linux shell命令组合
- 精选37条强大的常用linux shell命令组合
- Linux下shell的远程协助
- Linux shell 远程协助
- linux 远程shell 实现
- Linux 通过shell远程调用
- Linux Shell远程交互命令
- linux shell 远程执行命令
- Linux系统远程登录Shell
- linux 远程文件同步(shell)
- Linux shell 远程执行命令
- Linux的 cut用法精选
- 利用SharpSsh远程执行linux的shell命令
- [转]【OpenCV入门教程之七】 玩转OpenCV源代码:生成OpenCV工程解决方案与OpenCV源码编译
- 动画
- $.ajax的使用
- CentOS的free命令
- Tomcat 安全配置
- java远程linux的shell(精选)
- 利用tensorflow训练自己的图片数据(3)——建立网络模型
- tomcat如何配置环境变量
- spring boot实战目录
- 个人信息表:(增删查)
- MyBanner
- 用typeToken解析json串
- iOS的计算获取本月前几个或者后几个月份
- Linux进程间通信 —— 6种机制简介