SSH.NET How to unpack on the fly

I have a 2Gb image image.gz. Now I am doing the following steps to get unpacked image.gzon the target machine:

  • Download image.gzwith Renci.SshNet.ScpClient.
  • Uncompress the file by running the remote Renci.SshNet.SshCommand.

These 2 steps take about 4 minutes, and I want to speed up this process.

If I could use a simple command line, I would simply write the following command:
cat image.gz | ssh 10.70.0.1 "gunzip -C - > /var/tmp/decompressed_image"

This command completes after 2 minutes, which is twice as fast as before.


I tried to implement this single-line key using Renci.SSH:

  sshClient.Connect();

  var shellStream = sshClient.CreateShellStream(
      terminalName: "dumb",
      columns: 80,
      rows: 24,
      width: 800,
      height: 600,
      bufferSize: 1024);

  while (!shellStream.CanWrite)
  {
      Thread.Sleep(TimeSpan.FromSeconds(1));
  }

  shellStream.WriteLine("cat - | gunzip -c > /var/tmp/image.raw");
  shellStream.Flush();

  long totalBytes = 0;
  const int bufferSize = 1024;
  using (var fileStream = File.OpenRead(@"D:\images\image.gz"))
  using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, bufferSize))
  {
      var buffer = new byte[1024];
      int bytesRead;
      while ((bytesRead = streamReader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
      {
          totalBytes += bytesRead;

          if (totalBytes % Megabyte == 0)
          {
              Console.WriteLine(totalBytes / Megabyte);
          }

          while (!shellStream.CanWrite)
          {
              Thread.Sleep(TimeSpan.FromSeconds(1));
          }

          shellStream.Write(buffer, 0, bytesRead);
      }
   }

As you may have guessed, this code does not work. That was my only idea of ​​how this could be implemented.

If you have the best ideas (work ideas :)), share them with me.

+4

Source: https://habr.com/ru/post/1676142/


All Articles