gpt4 book ai didi

java - 如何在 Secure_CRT 上远程编辑 zip 中的文件?

转载 作者:太空狗 更新时间:2023-10-29 11:17:00 24 4
gpt4 key购买 nike

我的任务是在 SecureCRT 上编辑 zip 内的文件。我能够使用 JSCH 库 (com.jcraft.jsch) 远程运行 Linux 命令

这是我的部分代码:

 Session session = setUpSession(testParameters, softAsserter);
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream inputStream = channel.getInputStream();
channel.connect();

我想知道什么是最好的方法或正确的命令来编辑 SecureCRT 服务器上 zip 文件内的文件(例如 Test.txt)。

最佳答案

可以通过多种方式修改 zip 文件中的内容。

我已经提到了一些可能对您有用的方法。为了做到这一点

我们应该安全地将源文件/编译文件从本地机器传输到服务器。以下链接将有助于安全地传输文件。

https://www.vandyke.com/int/drag_n_drop.html

作为第一步,我们应该开发一个能够修改 zip 文件内容的片段,然后我们应该将文件复制到服务器。然后我们执行命令来运行这个文件,这样zip里面的内容就被修改了。

下面提到的方法只是为了修改 zip 竞争。

方法一:使用简单的Java代码段实现

我们可以编写一个简单的 java 片段,它可以打开 zip 文件并编辑,将文件保存在机器中,然后通过运行“java filename”来执行类文件,这实际上会修改 zip 文件中的内容。

有帮助的链接: Modifying a text file in a ZIP archive in Java

import java.io.*;
import java.nio.file.*;

class RemoteEditFileContends {

/**
* Edits the text file in zip.
*
* @param zipFilePathInstance
* the zip file path instance
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void editTextFileInZip(String zipFilePathInstance) throws IOException {
Path pathInstance = Paths.get(zipFilePathInstance);
try (FileSystem fileSystemIns = FileSystems.newFileSystem(pathInstance, null)) {
Path pathSourceInstance = fileSystemIns.getPath("/abc.txt");
Path tempCopyIns = generateTempFile(fileSystemIns);
Files.move(pathSourceInstance, tempCopyIns);
streamCopy(tempCopyIns, pathSourceInstance);
Files.delete(tempCopyIns);
}
}

/**
* Generate temp file.
*
* @param fileSystemIns
* the file system ins
* @return the path
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static Path generateTempFile(FileSystem fileSystemIns) throws IOException {
Path tempCopyIns = fileSystemIns.getPath("/___abc___.txt");
if (Files.exists(tempCopyIns)) {
throw new IOException("temp file exists, generate another name");
}
return tempCopyIns;
}

/**
* Stream copy.
*
* @param sourecInstance
* the src
* @param destinationInstance
* the dst
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void streamCopy(Path sourecInstance, Path destinationInstance) throws IOException {
try (
BufferedReader bufferInstance = new BufferedReader(new InputStreamReader(Files.newInputStream(sourecInstance)));
BufferedWriter writerInstance = new BufferedWriter(
new OutputStreamWriter(Files.newOutputStream(destinationInstance)))) {
String currentLine = null;
while ((currentLine = bufferInstance.readLine()) != null) {
currentLine = currentLine.replace("key1=value1", "key1=value2");
writerInstance.write(currentLine);
writerInstance.newLine();
}
}
}

public static void main(String[] args) throws IOException {
editTextFileInZip("test.zip");
}

}

方法二:使用python修改zip文件

How to update one file inside zip file using python

方法三:写一个shell脚本,直接修改zip文件的内容,这样我们就可以把shell脚本拷贝到服务器上,然后直接执行shell脚本。 https://superuser.com/questions/647674/is-there-a-way-to-edit-files-inside-of-a-zip-file-without-explicitly-extracting

以下代码段将帮助您使用该库进行连接和执行。

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

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 ConnetionManager {

private static final Logger _logger = Logger.getLogger(ConnetionManager.class.getName());

private JSch jschSSHChannel;

private String strUserName;

private String strConnectionIP;

private int intConnectionPort;

private String strPassword;

private Session sesConnection;

private int intTimeOut;

private void doCommonConstructorActions(String userNameInstance, String tokenpassword, String connetionServerIo,
String hostFileName) {
jschSSHChannel = new JSch();
try {
jschSSHChannel.setKnownHosts(hostFileName);
} catch (JSchException exceptionInstance) {
_logError(exceptionInstance.getMessage());
}
strUserName = userNameInstance;
strPassword = tokenpassword;
strConnectionIP = connetionServerIo;
}

public ConnetionManager(String userName, String password, String connectionIP, String knownHostsFileName) {
doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName);
intConnectionPort = 22;
intTimeOut = 60000;
}

public ConnetionManager(String userName, String password, String connectionIP, String knownHostsFileName,
int connectionPort) {
doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName);
intConnectionPort = connectionPort;
intTimeOut = 60000;
}

public ConnetionManager(String userName, String password, String connectionIP, String knownHostsFileName,
int connectionPort, int timeOutMilliseconds) {
doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName);
intConnectionPort = connectionPort;
intTimeOut = timeOutMilliseconds;
}

public String connect() {
String errorMessage = null;
try {
sesConnection = jschSSHChannel.getSession(strUserName, strConnectionIP, intConnectionPort);
sesConnection.setPassword(strPassword);
sesConnection.connect(intTimeOut);
} catch (JSchException exceptionInstance) {
errorMessage = exceptionInstance.getMessage();
}
return errorMessage;
}

private String _logError(String errorMessage) {
if (errorMessage != null) {
_logger.log(Level.SEVERE, "{0}:{1} - {2}", new Object[] { strConnectionIP, intConnectionPort, errorMessage });
}
return errorMessage;
}

private String _logWarnings(String warnMessage) {
if (warnMessage != null) {
_logger.log(Level.WARNING, "{0}:{1} - {2}", new Object[] { strConnectionIP, intConnectionPort, warnMessage });
}
return warnMessage;
}

public String sendCommand(String executionCommand) {
StringBuilder outputBuffer = new StringBuilder();
try {
Channel channelInstance = sesConnection.openChannel("exec");
((ChannelExec) channelInstance).setCommand(executionCommand);
InputStream commandOutputStream = channelInstance.getInputStream();
channelInstance.connect();
int readByte = commandOutputStream.read();
while (readByte != 0xffffffff) {
outputBuffer.append((char) readByte);
readByte = commandOutputStream.read();
}
channelInstance.disconnect();
} catch (IOException ioExceptionInstance) {
_logWarnings(ioExceptionInstance.getMessage());
return null;
} catch (JSchException schExceptionInstance) {
_logWarnings(schExceptionInstance.getMessage());
return null;
}
return outputBuffer.toString();
}

public void close() {
sesConnection.disconnect();
}

}

关于java - 如何在 Secure_CRT 上远程编辑 zip 中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55356407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com