一、示例如下:
1、执行远程脚本命令如下:
执行的脚本文件为/home/pythonwork/目录下的operate_parameters.sh 参数1:“hello” 参数2:“zhangsan” 结果返回1:脚本执行成功了 结果返回0:脚本执行失败了
sh /home/pythonwork/operate_parameters.sh "hello" "zhangsan"
二、java代码如下:
1、引入ganymed-ssh2-build210.jar包
org.jvnet.hudson
ganymed-ssh2
build210-hudson-1
commons-lang
commons-lang
2.6
2、编写调用远程脚本的工具类,可直接复制使用
package core.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.apache.commons.lang.StringUtils;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class RemoteShellTool {
private Connection conn;
private String ipAddr;
private String charset = Charset.defaultCharset().toString();
private String userName;
private String password;
public RemoteShellTool(String ipAddr, String userName, String password,
String charset) {
this.ipAddr = ipAddr;
this.userName = userName;
this.password = password;
if (charset != null) {
this.charset = charset;
}
}
/**
*
* @Title: login
* @Description: 用户名密码方式 远程登录linux服务器
* @return: Boolean
* @throws
*/
public boolean login(){
boolean flag = false;
try {
conn = new Connection(ipAddr);
conn.connect(); // 连接
flag = conn.authenticateWithPassword(userName, password); // 认证
if (flag) {
System.out.println("=========登陆xxx服务器的用户名和密码认证成功!");
} else {
System.out.println("=========登陆xxx服务器的用户名和密码认证失败!");;
conn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**
*
* @Title: processStdout
* @Description: 解析脚本执行的返回结果
* @param in 输入流对象
* @param charset 编码
* @return String 以纯文本的格式返回
* @throws
*/
public 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();
}
/**
* @author Ickes
* 远程执行shll脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行完后返回的结果值,如果命令执行失败,返回空字符串,不是null
* @since V0.1
*/
public String exec(String cmds) {
String result = "";
try {
if (this.login()) {
Session session = conn.openSession(); // 打开一个会话
session.execCommand(cmds);
result = this.processStdout(session.getStdout(), this.charset);
if(StringUtils.isBlank(result)){// 如果为得到标准输出为空字符串,说明脚本执行出错了
result = processStdout(session.getStderr(),this.charset);
System.out.println("-----脚本执行出错了,返回空空字符串-----:"+result);
}
session.close();
conn.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
return result;
}
}
3、编写测试类
public static void main(String[] args) {
/**=============拼接脚本命令start==========================*/
StringBuffer stringBuffer = new StringBuffer("sh /home/pythonwork/operate_parameters.sh ");
String param1="hello";
String param2="zhangsan";
if(param1!=null && !"".equals(param1)){
stringBuffer.append("\""+param1.trim()+"\" ");
};
if(param2!=null && !"".equals(param2)){
stringBuffer.append("\""+param2.trim()+"\"");
};
String str=stringBuffer.toString();
System.out.println(str);
/**=============拼接脚本命令end==========================*/
/**=============调用远程工具类start==========================*/
RemoteShellTool tool = new RemoteShellTool("192.168.0.1", "111111",
"111111", "utf-8");//调用工具类,参数1为远程电脑的ip,参数2位用户名,参数3位密码,参数4位编码方式
String result = tool.exec(str);//返回执行结果
/**=============调用远程工具类end==========================*/
if(result.contains("1")){//说明脚本执行成功,返回执行结果1
System.out.println("-----脚本执行成功了,返回的结果值为-----:"+result);
}else if(result.contains("0")){// 说明脚本执行失败,返回执行结果0
System.out.println("-----脚本执行失败了,返回的结果值为-----:"+result);
}
System.out.println(“------输出返回结果--------: ”+result);
}
4、如何连接成功,执行的效果如下:
=========登陆xxx服务器的用户名和密码认证成功!
执行远程脚本的命令======:sh /home/pythonwork/operate_parameters.sh "hello" "zhangsan"
-----脚本执行成功了,返回的结果值为-----:1
------输出返回结果--------: 1