调用远程linux服务器shell脚本

package com.haiyisoft.hyoaPc.ui;

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;
}
public static void main(String[] args) {
RemoteExecuteCommand rec=new RemoteExecuteCommand("172.20.188.157", "root","[email protected]");
//执行命令
//System.out.println(rec.execute("cd /data/app"));

//执行脚本
//System.out.println(rec.execute("/bin/sh /data/app/cassistant/startbatch.sh start cAssistantrightweb"));
//System.out.println(rec.execute("/bin/sh /data/app/cassistant/ice.sh"));
System.out.println(rec.execute("ps -ef|grep icepatch2server "));
//System.out.println(rec.execute("/bin/sh /data/app/test.sh"));
//这个方法与上面最大的区别就是,上面的方法,不管执行成功与否都返回,
//这个方法呢,如果命令或者脚本执行错误将返回空字符串
// rec.executeSuccess("ifconfig");

}
}

原文地址:https://www.cnblogs.com/zhangzhiqin/p/10278497.html

时间: 2024-08-29 19:16:26

调用远程linux服务器shell脚本的相关文章

Linux生产服务器Shell脚本分享

Linux生产服务器Shell脚本分享 2012-6-6 86市场网 linux 作为一名Linux/unix系统管理员,我经常遇到人问这个问题:shell能做什么?PHP这么强大,为什么不用PHP来完成工作呢?其实相对于PHP这些开发语言而言,shell主要用于数据库备份(SVN备份).计划任务(crontab).服务状态监控.FTP远程备份等.对于这些任务,shell的强大是大家都公认的,这也是每一个Linux/unix系统管理员的基本之一.现在在Windows 2008里也出现了Power

Linux/Unix shell 脚本中调用SQL,RMAN脚本

Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可少的技能之一.本文针对Linux/Unix shell脚本调用sql, rman 脚本给出了相关示例. 一.由shell脚本调用sql,rman脚本 [python] view plain copy print? 1.shell脚本调用sql脚本 #首先编辑sql文件 [email protecte

详解Linux交互式shell脚本中创建对话框实例教程_linux服务器

本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息对话框弹出,需要你的输入.对话框的类型有密码箱,检查表,菜单,等等.他们可以引导你以一种直观的方式输入必要的信息,使用这样的用户友好的对话框的好处是显而易见的.如下图所示: 当你写一个交互式shell脚本,你可以使用这样的对话框来接受用户的输入.whiptail可以在shell脚本中创建基于终端的对

自动发送密码抓取远程日志用Shell脚本如何实现?

在linux系统中,如何用shell脚本抓取远程日志?分析线上的日志会有一个困境,机器太多,如果每台都登录上去看不太现实,但是下载下来更麻烦因为每台SCP都要求输入密码.于是我便写了一个自动抓取远程日志的脚本,实现在基本功能. 代码: #!/usr/bin/expect -f if { $argc !=1 && $argc !=2 } {send_user "没有输入远程日志文件名.\n"send_user "格式是:get.sh ${remote_log}

Linux常用Shell脚本

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

linux学习—shell脚本知识点总结

Shell脚本 --Shell脚本有点像早期的批处理文件,将一些命令汇总起来一次执行,但Shell有更强大的功能,可以类似于程序的编写,不需要编译就能执行,使用起来很方便,可以简化我们日常的管理工作. 目录 什么是Shell 脚本的运行方法 #!(读shabang)的作用详解 结合案例分析变量类型 特殊变量及其[email protected] $*的区别 退出状态 算数运算 test和[ expression ]判断 一.什么是Shell 我们知道管理整个计算机硬件的其实是核心(kernel)

Idea 2019.2 (Ultimate Edition) 部署项目到远程Linux服务器tomcat 详解

前言:之前做项目,一直都是把本地的源码打成war包上传到远程服务器tomcat/webapps下.每次都要重新打成war进行项目的部署,感觉特别繁琐. 步骤: 1.环境配置 前提:保证在远程linux服务器上安装好tomcat. 2.Linux服务器tomcat配置 (1)在Linux上找到我们安装的tomcat的目录: (2)在tomcat下的bin中找到catalina.sh,进行远程发布需要在catalina.sh这个启动脚本中增加一些配置: 编辑catalina.sh文件: 按inset

linux下shell脚本执行方法及exec和source命令

exec和source都属于bash内部命令(builtins commands),在bash下输入man exec或man source可以查看所有的内部命令信息. bash shell的命令分为两类:外部命令和内部命令.外部命令是通过系统调用或独立的程序实现的,如sed.awk等等.内部命令是由特殊的文件格式(.def)所实现,如cd.history.exec等等. 在说明exe和source的区别之前,先说明一下fork的概念. fork是linux的系统调用,用来创建子进程(child

Linux系统shell脚本编程——生产实战案例

Linux系统shell脚本编程--生产实战案例     在日常的生产环境中,可能会遇到需要批量检查内网目前在线的主机IP地址有哪些,还可能需要检查这些在线的主机哪些端口是开放状态,因此依靠手工来检查是可以实现,但比较费时费力,所以需要结合shell脚本来实现批量检查的功能,那么今天就来做个小小的实验. 1.开发脚本前准备 一般大家都知道,测试主机是否在线,常用的命令无非就是ping.nmap,因此,首先找一个地址来测试下ping命令的效果 [[email protected] scripts]