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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
?Java數(shù)據(jù)結(jié)構(gòu)的十大排序
這篇文章主要介紹了?Java數(shù)據(jù)結(jié)構(gòu)的十大排序,排序算法分為比較類排序和非比較類排序,具體的內(nèi)容,需要的朋友參考下面思維導(dǎo)圖及文章介紹,希望對(duì)你有所幫助2022-01-01Spring ApplicationContextAware 接口的作用及使用方式
Spring提供了許多回調(diào)接口,用于Bean生命周期中執(zhí)行特定的操作,通過(guò)實(shí)現(xiàn)ApplicationContextAware接口,Spring提供了一種便捷的方式讓 Bean獲取對(duì)Spring容器的引用,本文介紹ApplicationContextAware接口的作用、使用方式,以及在實(shí)際應(yīng)用中的常見(jiàn)場(chǎng)景,感興趣的朋友一起看看吧2024-01-01Java基本概念監(jiān)視器實(shí)習(xí)原理解析
這篇文章主要介紹了Java基本概念監(jiān)視器實(shí)習(xí)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08ImportBeanDefinitionRegistrar手動(dòng)控制BeanDefinition創(chuàng)建注冊(cè)詳解
這篇文章主要為大家介紹了ImportBeanDefinitionRegistrar手動(dòng)控制BeanDefinition創(chuàng)建注冊(cè)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12SpringBoot中dubbo+zookeeper實(shí)現(xiàn)分布式開(kāi)發(fā)的應(yīng)用詳解
這篇文章主要介紹了SpringBoot中dubbo+zookeeper實(shí)現(xiàn)分布式開(kāi)發(fā)的應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Java跳出當(dāng)前的多重嵌套循環(huán)的五種方法
在Java編程中,跳出多重嵌套循環(huán)可以使用break語(yǔ)句、標(biāo)號(hào)與break組合、return語(yǔ)句、標(biāo)志變量和異常處理五種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10Spring Boot環(huán)境下Mybatis Plus的快速應(yīng)用操作
這篇文章主要介紹了Spring Boot環(huán)境下Mybatis Plus的快速應(yīng)用操作,具有很好的價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11