最近接了个需求,要求远程调shell脚本,你没听错!!!需求就一句话,咱是谁,咱是优秀的开发选手。考虑再三,有两种实现方式:
方案一:脚本所在服务器安装一个客户端,也就是自己写的一个小程序,本地通过端口调目标服务器的程序,然后程序调本机上的shell脚本!
优点:通过端口调用,用户不用暴露服务器的账号密码,安全性高
缺点:我们需要一直维护这个客户端程序,而且每接入一台服务器,都得安装该客户端,另外非常考验客户端程序的健壮性。
方案二:本地直接通过ip,服务器账号密码调远程服务器的shell脚本
优点:代码易开发,扩展时只用扩展服务端代码即可
缺点:用户服务器的账号密码会暴露给服务端,密码安全问题
把每种方案的优缺点汇报给leader,leader说:按第二种来吧
来吧!!开干,废话不多说,直接上代码:
导入程序所需的软件包:
1 2 3 4 5 |
<dependency> <groupid>org.jvnet.hudson</groupid> <artifactid>ganymed-ssh2</artifactid> <version>build210-hudson- 1 </version> </dependency> |
程序涉及的demo:
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import java.io.ioexception; import java.io.inputstream; import java.io.unsupportedencodingexception; import java.nio.charset.charset;
import org.apache测试数据mons.io.ioutils;
import ch.ethz.ssh2.channelcondition; import ch.ethz.ssh2.connection; import ch.ethz.ssh2.session; import ch.ethz.ssh2.streamgobbler;
public class remoteshellexecutor {
private connection conn; /** 远程机器ip */ private string ip; /** 用户名 */ private string osusername; /** 密码 */ private string password; private string charset = charset.defaultcharset().tostring();
private final string get_shell_pid = "ps -ef | grep '%s' | grep -v grep |awk '{print $2}'" ;
private final string kill_shell_pid = "kill -15 %s" ;
private static final int time_out = 1000 * 5 * 60 ;
/** * 构造函数 * @param ip * @param usr * @param pasword */ public remoteshellexecutor(string ip, string usr, string pasword) { this .ip = ip; this .osusername = usr; this .password = pasword; }
/** * 登录 * @return * @throws ioexception */ private boolean login() throws ioexception { conn = new connection(ip); conn.connect(); return conn.authenticatewithpassword(osusername, password); }
/** * 执行脚本 * * @param cmds * @return * @throws exception */ public executeresultvo exec(string cmds) throws exception { inputstream stdout = null ; inputstream stderr = null ; executeresultvo executeresultvo = new executeresultvo(); string outstr = "" ; string outerr = "" ; 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(cmds);
stdout = new streamgobbler(session.getstdout()); outstr = processstream(stdout, charset);
stderr = new streamgobbler(session.getstderr()); outerr = processstream(stderr, charset);
session.waitforcondition(channelcondition.exit_status, time_out);
system.out.println( "outstr=" + outstr); system.out.println( "outerr=" + outerr);
ret = session.getexitstatus(); executeresultvo.setoutstr(outstr); executeresultvo.setouterr(outerr);
} else { throw new exception( "登录远程机器失败" + ip); // 自定义异常类 实现略 } } finally { if (conn != null ) { conn.close(); } ioutils.closequietly(stdout); ioutils.closequietly(stderr); } return ret; }
/** * @param in * @param charset * @return * @throws ioexception * @throws unsupportedencodingexception */ private string processstream(inputstream in, string charset) throws exception { byte [] buf = new byte [ 1024 ]; stringbuilder sb = new stringbuilder(); int len = 0 ; while ((len=in.read(buf)) != - 1 ) { sb.append( new string(buf, 0 ,len, charset)); } return sb.tostring(); }
public static void main(string args[]) throws exception { //调远程shell remoteshellexecutor executor = new remoteshellexecutor( "192.168.234.123" , "root" , "beebank" ); system.out.println(executor.exec( "sh /data/checkmysql.sh" )); //获取远程shell 进程 pid executeresultvo executeresultvo = executor.exec(string.format(get_shell_pid, "sh /data/checkmysql.sh" )); //杀掉shell进程 executeresultvo executeresultvo1 = executor.exec(string.format(kill_shell_pid ,executeresultvo.getoutstr()));
}
public class executeresultvo<t>{ private string outstr; private string outerr; //省略get set } } |
经过测试也确实好用啊,大家可以根据这个demo进行相应的修改。到此这篇关于java调远程服务器的shell脚本以及停止的方法实现的文章就介绍到这了,更多相关java调远程shell脚本内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/qq_37191371/article/details/114934794
查看更多关于java调用远程服务器的shell脚本以及停止的方法实现的详细内容...