java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例
java通過ssh連接服務(wù)器執(zhí)行shell命令詳解
java通過ssh連接服務(wù)器執(zhí)行shell命令:JSch 是SSH2的一個純Java實(shí)現(xiàn)。它允許你連接到一個sshd 服務(wù)器,使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)鹊?。你可以將它的功能集成到你自己?程序中。同時該項(xiàng)目也提供一個J2ME版本用來在手機(jī)上直連SSHD服務(wù)器。
SSH是Secure Shell的縮寫,一種建立在應(yīng)用層和傳輸層基礎(chǔ)上的安全協(xié)議。SSH在連接和傳送過程中會加密所有數(shù)據(jù),可以用來在不同系統(tǒng)或者服務(wù)器之間進(jìn)行安全連接。SSH提供兩種的安全驗(yàn)證方式:基于密碼的認(rèn)證和基于密匙的認(rèn)證。其中,基于密碼的認(rèn)證比較簡單,只要知道遠(yuǎn)程主機(jī)的用戶名和密碼,就可以進(jìn)行登錄。基于密匙的認(rèn)證比較麻煩,而且連接比較耗時,這里不詳細(xì)介紹。
有很多基于SSH協(xié)議的客戶端,例如:PuTTY、OpenSSH、Xshell 4等,可以遠(yuǎn)程連接幾乎所有UNIX平臺。同時,可以通過Linux命令行ssh uername@host連接到某主機(jī)。
在項(xiàng)目中,如何利用代碼實(shí)現(xiàn)SSH,遠(yuǎn)程執(zhí)行Shell腳本呢?JSch是Java Secure Channel的縮寫,是一個SSH2功能的純Java實(shí)現(xiàn),具體信息可以參考JSch官網(wǎng)。它允許你連接到一個SSH服務(wù)器,并且可以使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)龋瑫r你也可以集成它的功能到你自己的應(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之后要主動打開連接
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)文章
Mybatis-Plus自動填充更新操作相關(guān)字段的實(shí)現(xiàn)
數(shù)據(jù)庫表中應(yīng)該都要有create_time、update_time字段;那么在開發(fā)中,對于這些共有字段的處理應(yīng)該要進(jìn)行統(tǒng)一,這樣就可以簡化我們的開發(fā)過程。那么本文就對Mybatis-Plus中的字段自動填充進(jìn)行記錄2021-11-11
Java中Caffeine本地緩存項(xiàng)目實(shí)例
這篇文章主要介紹了Java中Caffeine本地緩存項(xiàng)目實(shí)例,Caffeine是一個高性能Java 緩存庫,使用Java8對Guava緩存重寫版本,在Spring Boot 2.0中將取代Guava,使用spring.cache.cache-names屬性可以在啟動時創(chuàng)建緩存,需要的朋友可以參考下2023-10-10
SpringBoot集成Flyway進(jìn)行數(shù)據(jù)庫版本遷移管理的步驟
這篇文章主要介紹了SpringBoot集成Flyway進(jìn)行數(shù)據(jù)庫版本遷移管理的步驟,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-03-03
Spring注解Autowired的底層實(shí)現(xiàn)原理詳解
從當(dāng)前springboot的火熱程度來看,java?config的應(yīng)用是越來越廣泛了,在使用java?config的過程當(dāng)中,我們不可避免的會有各種各樣的注解打交道,其中,我們使用最多的注解應(yīng)該就是@Autowired注解了。本文就來聊聊Autowired的底層實(shí)現(xiàn)原理2022-10-10
泛談Java中的不可變數(shù)據(jù)結(jié)構(gòu)
開發(fā)人員通常認(rèn)為擁有final引用,或者val在Kotlin或Scala中,足以使對象不可變。這篇博客文章深入研究了不可變引用和不可變數(shù)據(jù)結(jié)構(gòu),下面小編來和大家一起學(xué)習(xí)它2019-05-05
Spring Boot2.0使用Spring Security的示例代碼
這篇文章主要介紹了Spring Boot2.0使用Spring Security的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

