java跳板執(zhí)行ssh命令方式
更新時間:2024年12月17日 10:25:46 作者:princeAladdin
本文分享了在Java中使用跳板機執(zhí)行SSH命令的方法,并推薦了一些Maven依賴,希望這些信息對大家有所幫助
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)建跳板會話 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)建目標服務器會話 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)閉通道和會話 channelExec.disconnect(); targetSession.disconnect(); jumpSession.disconnect(); } catch (JSchException | IOException e) { LOGGER.error("跳板連接服務器執(zhí)行異常",e); } return excuteResult; } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot2.0 整合 SpringSecurity 框架實現(xiàn)用戶權(quán)限安全管理方法
Spring Security是一個能夠為基于Spring的企業(yè)應用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。這篇文章主要介紹了SpringBoot2.0 整合 SpringSecurity 框架,實現(xiàn)用戶權(quán)限安全管理 ,需要的朋友可以參考下2019-07-07SpringBoot整合RabbitMQ實現(xiàn)六種工作模式的示例
這篇文章主要介紹了SpringBoot整合RabbitMQ實現(xiàn)六種工作模式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07MyBatis處理大字段或BLOB、CLOB類型數(shù)據(jù)方式
這篇文章主要介紹了MyBatis處理大字段或BLOB、CLOB類型數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04淺談java object對象在heap中的結(jié)構(gòu)
本文主要介紹了淺談java object對象在heap中的結(jié)構(gòu),感興趣的同學,可以參考下。2021-06-06Java畢業(yè)設計實戰(zhàn)之醫(yī)院心理咨詢問診系統(tǒng)的實現(xiàn)
這是一個使用了java+Spring+Maven+mybatis+Vue+mysql開發(fā)的醫(yī)院心理咨詢問診系統(tǒng),是一個畢業(yè)設計的實戰(zhàn)練習,具有心理咨詢問診該有的所有功能,感興趣的朋友快來看看吧2022-01-01