java跳板執(zhí)行ssh命令方式
java跳板執(zhí)行ssh命令
maven依賴
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>
import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; import java.lang.invoke.MethodHandles; public class SSHJumpServerUtil { private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public static String executeCommandWithJumpServer(String jumpHost, String jumpUser, String jumpPassword, int jumpPort, String targetHost, String targetUser, String targetPassword, int targetPort, String command) { String excuteResult = null; try { JSch jsch = new JSch(); // 創(chuàng)建跳板會(huì)話 Session jumpSession = jsch.getSession(jumpUser, jumpHost, jumpPort); jumpSession.setPassword(jumpPassword); jumpSession.setConfig("StrictHostKeyChecking", "no"); jumpSession.connect(); // 創(chuàng)建跳板通道 int forwardedPort = 10022; // 本地轉(zhuǎn)發(fā)端口 jumpSession.setPortForwardingL(forwardedPort, targetHost, targetPort); // 創(chuàng)建目標(biāo)服務(wù)器會(huì)話 Session targetSession = jsch.getSession(targetUser, "localhost", forwardedPort); targetSession.setPassword(targetPassword); targetSession.setConfig("StrictHostKeyChecking", "no"); targetSession.connect(); // 執(zhí)行命令 ChannelExec channelExec = (ChannelExec) targetSession.openChannel("exec"); channelExec.setCommand(command); InputStream in = channelExec.getInputStream(); channelExec.connect(); // 讀取命令輸出 byte[] tmp = new byte[1024]; while (true) { while (in.available() > 0) { int i = in.read(tmp, 0, 1024); if (i < 0) break; excuteResult = new String(tmp, 0, i); } if (channelExec.isClosed()) { if (in.available() > 0) continue; LOGGER.debug("exit-status: " + channelExec.getExitStatus()); break; } try { Thread.sleep(1000); } catch (Exception ee) { ee.printStackTrace(); } } // 關(guān)閉通道和會(huì)話 channelExec.disconnect(); targetSession.disconnect(); jumpSession.disconnect(); } catch (JSchException | IOException e) { LOGGER.error("跳板連接服務(wù)器執(zhí)行異常",e); } return excuteResult; } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot2.0 整合 SpringSecurity 框架實(shí)現(xiàn)用戶權(quán)限安全管理方法
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問(wèn)控制解決方案的安全框架。這篇文章主要介紹了SpringBoot2.0 整合 SpringSecurity 框架,實(shí)現(xiàn)用戶權(quán)限安全管理 ,需要的朋友可以參考下2019-07-07SpringBoot整合RabbitMQ實(shí)現(xiàn)六種工作模式的示例
這篇文章主要介紹了SpringBoot整合RabbitMQ實(shí)現(xiàn)六種工作模式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07MyBatis處理大字段或BLOB、CLOB類型數(shù)據(jù)方式
這篇文章主要介紹了MyBatis處理大字段或BLOB、CLOB類型數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04詳解基于MVC的數(shù)據(jù)查詢模塊進(jìn)行模糊查詢
這篇文章主要介紹了Java基于MVC的數(shù)據(jù)查詢模塊進(jìn)行模糊查詢,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Java 排序算法整合(冒泡,快速,希爾,拓?fù)洌瑲w并)
這篇文章主要介紹了Java 排序算法整合(冒泡,快速,希爾,拓?fù)?,歸并),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09java 獲取日期的幾天前,幾個(gè)月前和幾年前的實(shí)例
下面小編就為大家?guī)?lái)一篇java 獲取日期的幾天前,幾個(gè)月前和幾年前的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10淺談java object對(duì)象在heap中的結(jié)構(gòu)
本文主要介紹了淺談java object對(duì)象在heap中的結(jié)構(gòu),感興趣的同學(xué),可以參考下。2021-06-06Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之醫(yī)院心理咨詢問(wèn)診系統(tǒng)的實(shí)現(xiàn)
這是一個(gè)使用了java+Spring+Maven+mybatis+Vue+mysql開發(fā)的醫(yī)院心理咨詢問(wèn)診系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有心理咨詢問(wèn)診該有的所有功能,感興趣的朋友快來(lái)看看吧2022-01-01