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

How to edit a file inside a zip remotely on Secure_CRT?

我的任务是在 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 文件中的内容。

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

我们应该安全地将源 file/compiled 文件从本地计算机传输到服务器。以下 link 将有助于安全地传输文件。

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

作为第一步,我们应该开发一个能够修改 zip 文件内容的片段,然后我们应该将文件复制到服务器。然后我们对文件执行命令 运行 以便修改 zip 中的内容。

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

方法 1:使用简单的 Java 片段实现

我们可以编写一个简单的 java 片段,它可以打开 zip 文件并进行编辑,将文件保存在机器中,然后只需 运行ning [即可执行 class 文件 "java filename" 这实际上会修改 zip 文件中的内容。

Link 这会有所帮助: 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");
  }

}

方法 2:使用 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();
  }

}