java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實例
java通過ssh連接服務(wù)器執(zhí)行shell命令詳解
java通過ssh連接服務(wù)器執(zhí)行shell命令:JSch 是SSH2的一個純Java實現(xiàn)。它允許你連接到一個sshd 服務(wù)器,使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)鹊?。你可以將它的功能集成到你自己?程序中。同時該項目也提供一個J2ME版本用來在手機上直連SSHD服務(wù)器。
SSH是Secure Shell的縮寫,一種建立在應(yīng)用層和傳輸層基礎(chǔ)上的安全協(xié)議。SSH在連接和傳送過程中會加密所有數(shù)據(jù),可以用來在不同系統(tǒng)或者服務(wù)器之間進(jìn)行安全連接。SSH提供兩種的安全驗證方式:基于密碼的認(rèn)證和基于密匙的認(rèn)證。其中,基于密碼的認(rèn)證比較簡單,只要知道遠(yuǎn)程主機的用戶名和密碼,就可以進(jìn)行登錄。基于密匙的認(rèn)證比較麻煩,而且連接比較耗時,這里不詳細(xì)介紹。
有很多基于SSH協(xié)議的客戶端,例如:PuTTY、OpenSSH、Xshell 4等,可以遠(yuǎn)程連接幾乎所有UNIX平臺。同時,可以通過Linux命令行ssh uername@host連接到某主機。
在項目中,如何利用代碼實現(xiàn)SSH,遠(yuǎn)程執(zhí)行Shell腳本呢?JSch是Java Secure Channel的縮寫,是一個SSH2功能的純Java實現(xiàn),具體信息可以參考JSch官網(wǎng)。它允許你連接到一個SSH服務(wù)器,并且可以使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?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)程主機的ip地址 private String ip; //遠(yuǎn)程主機登錄用戶名 private String username; //遠(yuǎn)程主機的登錄密碼 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并且打開連接,因為創(chuàng)建session之后要主動打開連接 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()"); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
JVM堆內(nèi)存溢出后,其他線程是否可繼續(xù)工作的問題解析
這篇文章主要介紹了JVM 堆內(nèi)存溢出后,其他線程是否可繼續(xù)工作?,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08SpringBoot中整合JodConverter實現(xiàn)文件在線預(yù)覽功能
Spring Boot JodConverter是一個基于Spring Boot框架的文檔轉(zhuǎn)換工具,它使用JodConverter庫來實現(xiàn)文檔格式之間的轉(zhuǎn)換,本文主要介紹了SpringBoot中整合JodConverter實現(xiàn)文件在線預(yù)覽功能,需要的朋友可以參考下2024-04-04java中調(diào)用https請求忽略ssl證書認(rèn)證代碼示例
在網(wǎng)絡(luò)請求中經(jīng)常會遇到需要忽略證書認(rèn)證的情況,這篇文章主要介紹了java中調(diào)用https請求忽略ssl證書認(rèn)證的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問題及解決
這篇文章主要介紹了IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11基于Servlet實現(xiàn)技術(shù)問答網(wǎng)站系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于Servlet實現(xiàn)技術(shù)問答網(wǎng)站系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04