java調(diào)用遠(yuǎn)程服務(wù)器的shell腳本以及停止的方法實(shí)現(xiàn)
最近接了個(gè)需求,要求遠(yuǎn)程調(diào)shell腳本,你沒(méi)聽(tīng)錯(cuò)!?。⌒枨缶鸵痪湓?,咱是誰(shuí),咱是優(yōu)秀的開(kāi)發(fā)選手??紤]再三,有兩種實(shí)現(xiàn)方式:
方案一:腳本所在服務(wù)器安裝一個(gè)客戶端,也就是自己寫的一個(gè)小程序,本地通過(guò)端口調(diào)目標(biāo)服務(wù)器的程序,然后程序調(diào)本機(jī)上的shell腳本!
優(yōu)點(diǎn):通過(guò)端口調(diào)用,用戶不用暴露服務(wù)器的賬號(hào)密碼,安全性高
缺點(diǎn):我們需要一直維護(hù)這個(gè)客戶端程序,而且每接入一臺(tái)服務(wù)器,都得安裝該客戶端,另外非常考驗(yàn)客戶端程序的健壯性。
方案二:本地直接通過(guò)IP,服務(wù)器賬號(hào)密碼調(diào)遠(yuǎn)程服務(wù)器的shell腳本
優(yōu)點(diǎn):代碼易開(kāi)發(fā),擴(kuò)展時(shí)只用擴(kuò)展服務(wù)端代碼即可
缺點(diǎn):用戶服務(wù)器的賬號(hào)密碼會(huì)暴露給服務(wù)端,密碼安全問(wèn)題
把每種方案的優(yōu)缺點(diǎn)匯報(bào)給leader,leader說(shuō):按第二種來(lái)吧
來(lái)吧??!開(kāi)干,廢話不多說(shuō),直接上代碼:
導(dǎo)入程序所需的軟件包:
<dependency> <groupId>org.jvnet.hudson</groupId> <artifactId>ganymed-ssh2</artifactId> <version>build210-hudson-1</version></dependency>
程序涉及的demo:
import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset; import org.apache.commons.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; /** 遠(yuǎn)程機(jī)器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; /** * 構(gòu)造函數(shù) * @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); } /** * 執(zhí)行腳本 * * @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('登錄遠(yuǎn)程機(jī)器失敗' + ip); // 自定義異常類 實(shí)現(xiàn)略 } } 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 { //調(diào)遠(yuǎn)程shell RemoteShellExecutor executor = new RemoteShellExecutor('192.168.234.123', 'root', 'beebank'); System.out.println(executor.exec('sh /data/checkMysql.sh')); //獲取遠(yuǎn)程shell 進(jìn)程 pid ExecuteResultVO executeResultVO = executor.exec(String.format(GET_SHELL_PID,'sh /data/checkMysql.sh')); //殺掉shell進(jìn)程 ExecuteResultVO executeResultVO1 = executor.exec(String.format(KILL_SHELL_PID ,executeResultVO.getOutStr())); } public class ExecuteResultVO<T>{ private String outStr; private String outErr; //省略get set }}
經(jīng)過(guò)測(cè)試也確實(shí)好用啊,大家可以根據(jù)這個(gè)demo進(jìn)行相應(yīng)的修改。到此這篇關(guān)于java調(diào)遠(yuǎn)程服務(wù)器的shell腳本以及停止的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java調(diào)遠(yuǎn)程shell腳本內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)2. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. 如何在jsp界面中插入圖片4. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器5. phpstudy apache開(kāi)啟ssi使用詳解6. xml中的空格之完全解說(shuō)7. JSP之表單提交get和post的區(qū)別詳解及實(shí)例8. jsp文件下載功能實(shí)現(xiàn)代碼9. 詳解瀏覽器的緩存機(jī)制10. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令
