java 整合jsch使用 遠程交互服務器
java 通過jsch 遠程執行命令 jsch 主要是類似Xshell 只不過是代碼級別使用
而 Xshell使用界面化
jsch可以執行任何shell 腳本 但是弊端是執行一次必須要關閉當前會話 每次都要cd當前目錄
在執行相關命令 都是操作 channle 同時寫了一個cmd執行方法為的是執行linux命令
execCommandByJSch (…) 這個方法是比較重要的 session就是維護你當前會話和服務器進行交流的
channle相當於管道流 數據交互用的
依賴
<!-- sftp的依賴--> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>
這里是以前某個項目的一部分代碼
package com.ubix.infinite.business.maintain.util; import com.alibaba.fastjson.JSON; import com.jcraft.jsch.*; import com.ubix.infinite.business.maintain.constants.MaintainConstants; import com.ubix.infinite.business.maintain.pojo.MaiFtpServer; import com.ubix.infinite.business.maintain.pojo.MaiServerAndPath; import com.ubix.infinite.business.maintain.pojo.MaiServerSoftware; import com.ubix.infinite.business.maintain.task.recoder.SoftwareDbUtils; import com.ubix.infinite.common.core.contexts.SecurityContext; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import java.io.*; import java.util.*; @Slf4j public class Ftp { private static final String ENCODE_UTF_8 = "UTF-8"; private Date lastPushDate = null; private static final String HISTORY_FOLDER = "history_server"; private Session sshSession; private ChannelSftp channel; public Ftp(String host, int port, String username, String password) throws Exception { connection(host, port, username, password); } public Ftp(MaiFtpServer maiFtpServer) throws Exception { String username = maiFtpServer.getUsername(); String password = maiFtpServer.getPassword(); String host = maiFtpServer.getHost(); int port = maiFtpServer.getPort(); connection(host, port, username, password); } /** * 鏈接服務器 * @param host * @param port * @param username * @param password * @throws Exception */ public void connection(String host, int port, String username, String password) throws Exception { JSch jsch = new JSch(); jsch.getSession(username, host, port); //根據用戶名,密碼,端口號獲取session sshSession = jsch.getSession(username, host, port); sshSession.setPassword(password); //修改服務器/etc/ssh/sshd_config 中 GSSAPIAuthentication的值yes為no,解決用戶不能遠程登錄 sshSession.setConfig("userauth.gssapi-with-mic", "no"); //為session對象設置properties,第一次訪問服務器時不用輸入yes sshSession.setConfig("StrictHostKeyChecking", "no"); sshSession.connect(); //獲取sftp通道 channel = (ChannelSftp) sshSession.openChannel("sftp"); channel.connect(); log.info("連接ftp成功!"); } /** * 關閉通道 */ public void closeChannel() { if (null != channel) { try { channel.disconnect(); } catch (Exception e) { log.error("關閉SFTP通道發生異常:", e); } } if (null != sshSession) { try { sshSession.disconnect(); } catch (Exception e) { log.error("SFTP關閉 session異常:", e); } } } /** * @param directory 上傳ftp的目錄 * @param uploadFile 本地文件目錄 * @param isDel 是否刪除原文件 */ public Hashtable<String, Object> upload(String directory, String uploadFile, boolean isDel, MaiFtpServer maiFtpServer) { Hashtable<String, Object> collectionResult = new Hashtable<>(); try { cdDirectory(directory); collectionResult = toUpload(directory, uploadFile, isDel, maiFtpServer); } catch (Exception e) { log.info("上傳失敗"); } return collectionResult; } /** * 根據當前路徑獲取目錄文件 * @param directory * @return */ public String getSonDirectory(String directory) { String dir = directory.substring(0, directory.lastIndexOf("/") + 1); try { channel.cd(dir); } catch (SftpException e) { log.error(e.getMessage()); } return "true"; } /** * 獲取文件夾下的文件 * * @param directory 路徑 */ public Vector<?> listFiles(String directory) { try { if (isDirExist(directory)) { Vector<?> vector = channel.ls(directory); //移除上級目錄和根目錄:"." ".." vector.remove(0); vector.remove(