sshd - modric2jeff/archive GitHub Wiki
`import org.apache.sshd.client.SshClient; import org.apache.sshd.client.subsystem.sftp.SftpClient; import org.apache.sshd.client.subsystem.sftp.VirtualFileSystemFactory; import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Paths;
public class SFTPFileDownloader {
public static void main(String[] args) {
String host = "remote-server-ip"; // 远程服务器 IP 地址
String user = "username"; // SSH 用户名
String password = "password"; // SSH 密码
String remoteFilePath = "/path/to/remote/logfile.log"; // 远程日志文件路径
// 使用 Apache SSHd 连接到远程服务器并通过 SFTP 获取文件内容
byte[] fileBytes = downloadFileAsBytes(host, user, password, remoteFilePath);
// 输出文件的前 100 个字节(仅作为演示,实际应用中根据需要处理文件)
if (fileBytes != null) {
System.out.println("File downloaded, first 100 bytes: ");
for (int i = 0; i < Math.min(100, fileBytes.length); i++) {
System.out.print(fileBytes[i] + " ");
}
}
}
public static byte[] downloadFileAsBytes(String host, String user, String password, String remoteFilePath) {
SshClient sshClient = SshClient.setUpDefaultClient();
sshClient.start();
try {
// 通过 SSH 客户端进行认证
sshClient.connect(user, host, 22)
.verify()
.getSession()
.addPasswordIdentity(password)
.auth();
// 获取 SFTP 客户端
try (SftpClient sftpClient = sshClient.createSftpClient()) {
// 获取文件的输入流
InputStream inputStream = sftpClient.open(remoteFilePath);
// 将输入流转换为 byte 数组
return convertInputStreamToByteArray(inputStream);
} catch (IOException e) {
System.err.println("Failed to download file via SFTP: " + e.getMessage());
}
} catch (IOException e) {
System.err.println("SSH connection failed: " + e.getMessage());
} finally {
sshClient.stop();
}
return null;
}
public static byte[] convertInputStreamToByteArray(InputStream inputStream) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096]; // 4KB 缓冲区
int bytesRead;
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
byteArrayOutputStream.flush();
} catch (IOException e) {
System.err.println("Failed to read from input stream: " + e.getMessage());
}
return byteArrayOutputStream.toByteArray();
}
} `
import com.jcraft.jsch.*;
import java.io.BufferedReader; import java.io.InputStreamReader;
public class SSHLogFetcherWithOffsetLimit {
public static void main(String[] args) {
String host = "remote-server-ip"; // 远程服务器 IP 地址
String user = "username"; // SSH 用户名
String password = "password"; // SSH 密码
String logFilePath = "/path/to/logfile.log"; // 日志文件路径
int offset = 1000; // 偏移量(跳过的行数)
int limit = 200; // 最大返回行数(最多返回多少行日志)
Session session = null;
ChannelExec channel = null;
try {
// 创建 JSch 对象
JSch jsch = new JSch();
// 创建 session
session = jsch.getSession(user, host, 22);
session.setPassword(password);
// 禁用主机密钥检查
session.setConfig("StrictHostKeyChecking", "no");
// 连接到远程主机
session.connect();
// 创建执行命令的通道
channel = (ChannelExec) session.openChannel("exec");
// 构建命令:跳过前 offset 行,并获取后 limit 行
String command = "sed -n '" + (offset + 1) + "," + (offset + limit) + "p' " + logFilePath;
channel.setCommand(command);
// 获取命令的输出
BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
// 连接通道
channel.connect();
// 输出日志内容
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭通道和会话
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
}