java通過(guò)ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例
java通過(guò)ssh連接服務(wù)器執(zhí)行shell命令詳解
java通過(guò)ssh連接服務(wù)器執(zhí)行shell命令:JSch 是SSH2的一個(gè)純Java實(shí)現(xiàn)。它允許你連接到一個(gè)sshd 服務(wù)器,使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)鹊取D憧梢詫⑺墓δ芗傻侥阕约旱?程序中。同時(shí)該項(xiàng)目也提供一個(gè)J2ME版本用來(lái)在手機(jī)上直連SSHD服務(wù)器。
SSH是Secure Shell的縮寫,一種建立在應(yīng)用層和傳輸層基礎(chǔ)上的安全協(xié)議。SSH在連接和傳送過(guò)程中會(huì)加密所有數(shù)據(jù),可以用來(lái)在不同系統(tǒng)或者服務(wù)器之間進(jìn)行安全連接。SSH提供兩種的安全驗(yàn)證方式:基于密碼的認(rèn)證和基于密匙的認(rèn)證。其中,基于密碼的認(rèn)證比較簡(jiǎn)單,只要知道遠(yuǎn)程主機(jī)的用戶名和密碼,就可以進(jìn)行登錄?;诿艹椎恼J(rèn)證比較麻煩,而且連接比較耗時(shí),這里不詳細(xì)介紹。
有很多基于SSH協(xié)議的客戶端,例如:PuTTY、OpenSSH、Xshell 4等,可以遠(yuǎn)程連接幾乎所有UNIX平臺(tái)。同時(shí),可以通過(guò)Linux命令行ssh uername@host連接到某主機(jī)。
在項(xiàng)目中,如何利用代碼實(shí)現(xiàn)SSH,遠(yuǎn)程執(zhí)行Shell腳本呢?JSch是Java Secure Channel的縮寫,是一個(gè)SSH2功能的純Java實(shí)現(xiàn),具體信息可以參考JSch官網(wǎng)。它允許你連接到一個(gè)SSH服務(wù)器,并且可以使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)?,同時(shí)你也可以集成它的功能到你自己的應(yīng)用程序。在使用前,需要下載并導(dǎo)入JSch包:jsch-0.1.50.jar。
示例程序
package com.stormma.demo; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; public class Shell { //遠(yuǎn)程主機(jī)的ip地址 private String ip; //遠(yuǎn)程主機(jī)登錄用戶名 private String username; //遠(yuǎn)程主機(jī)的登錄密碼 private String password; //設(shè)置ssh連接的遠(yuǎn)程端口 public static final int DEFAULT_SSH_PORT = 22; //保存輸出內(nèi)容的容器 private ArrayList<string> stdout; /** * 初始化登錄信息 * @param ip * @param username * @param password */ public Shell(final String ip, final String username, final String password) { this.ip = ip; this.username = username; this.password = password; stdout = new ArrayList<string>(); } /** * 執(zhí)行shell命令 * @param command * @return */ public int execute(final String command) { int returnCode = 0; JSch jsch = new JSch(); MyUserInfo userInfo = new MyUserInfo(); try { //創(chuàng)建session并且打開連接,因?yàn)閯?chuàng)建session之后要主動(dòng)打開連接 Session session = jsch.getSession(username, ip, DEFAULT_SSH_PORT); session.setPassword(password); session.setUserInfo(userInfo); session.connect(); //打開通道,設(shè)置通道類型,和執(zhí)行的命令 Channel channel = session.openChannel("exec"); ChannelExec channelExec = (ChannelExec)channel; channelExec.setCommand(command); channelExec.setInputStream(null); BufferedReader input = new BufferedReader(new InputStreamReader (channelExec.getInputStream())); channelExec.connect(); System.out.println("The remote command is :" + command); //接收遠(yuǎn)程服務(wù)器執(zhí)行命令的結(jié)果 String line; while ((line = input.readLine()) != null) { stdout.add(line); } input.close(); // 得到returnCode if (channelExec.isClosed()) { returnCode = channelExec.getExitStatus(); } // 關(guān)閉通道 channelExec.disconnect(); //關(guān)閉session session.disconnect(); } catch (JSchException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return returnCode; } /** * get stdout * @return */ public ArrayList<string> getStandardOutput() { return stdout; } public static void main(final String [] args) { Shell shell = new Shell("xxx.xxx.xxx.xxx", "username", "password"); shell.execute("uname -s -r -v"); ArrayList<string> stdout = shell.getStandardOutput(); for (String str : stdout) { System.out.println(str); } } }
MyUserInfo
package com.stormma.demo; import com.jcraft.jsch.UserInfo; public class MyUserInfo implements UserInfo { @Override public String getPassphrase() { // TODO Auto-generated method stub System.out.println("MyUserInfo.getPassphrase()"); return null; } @Override public String getPassword() { // TODO Auto-generated method stub System.out.println("MyUserInfo.getPassword()"); return null; } @Override public boolean promptPassphrase(String arg0) { // TODO Auto-generated method stub System.out.println("MyUserInfo.promptPassphrase()"); System.out.println(arg0); return false; } @Override public boolean promptPassword(String arg0) { // TODO Auto-generated method stub System.out.println("MyUserInfo.promptPassword()"); System.out.println(arg0); return false; } @Override public boolean promptYesNo(String arg0) { // TODO Auto-generated method stub' System.out.println("MyUserInfo.promptYesNo()"); System.out.println(arg0); if (arg0.contains("The authenticity of host")) { return true; } return true; } @Override public void showMessage(String arg0) { // TODO Auto-generated method stub System.out.println("MyUserInfo.showMessage()"); } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
JVM堆內(nèi)存溢出后,其他線程是否可繼續(xù)工作的問(wèn)題解析
這篇文章主要介紹了JVM 堆內(nèi)存溢出后,其他線程是否可繼續(xù)工作?,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Java Socket編程實(shí)例(一)- TCP基本使用
這篇文章主要講解Java Socket編程中TCP的基本使用,希望能給大家做一個(gè)參考。2016-06-06SpringBoot中整合JodConverter實(shí)現(xiàn)文件在線預(yù)覽功能
Spring Boot JodConverter是一個(gè)基于Spring Boot框架的文檔轉(zhuǎn)換工具,它使用JodConverter庫(kù)來(lái)實(shí)現(xiàn)文檔格式之間的轉(zhuǎn)換,本文主要介紹了SpringBoot中整合JodConverter實(shí)現(xiàn)文件在線預(yù)覽功能,需要的朋友可以參考下2024-04-04java中調(diào)用https請(qǐng)求忽略ssl證書認(rèn)證代碼示例
在網(wǎng)絡(luò)請(qǐng)求中經(jīng)常會(huì)遇到需要忽略證書認(rèn)證的情況,這篇文章主要介紹了java中調(diào)用https請(qǐng)求忽略ssl證書認(rèn)證的相關(guān)資料,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10Java靜態(tài)代理與動(dòng)態(tài)代理案例詳解
這篇文章主要介紹了Java靜態(tài)代理與動(dòng)態(tài)代理案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問(wèn)題及解決
這篇文章主要介紹了IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11基于Servlet實(shí)現(xiàn)技術(shù)問(wèn)答網(wǎng)站系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于Servlet實(shí)現(xiàn)技術(shù)問(wèn)答網(wǎng)站系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Mybatis深度整合Mysql的Json字段問(wèn)題
這篇文章主要介紹了Mybatis深度整合Mysql的Json字段問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12