java 整合jsch使用 远程交互服务器
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
