This is the 15th day of my participation in the August More Text Challenge. For details, see:August is more challenging

  • 📢 welcome to like: 👍 collect ⭐ message 📝 If there is any mistake please correct it, send roses, hand left fragrance!
  • 📢 This article was originally written by Webmote and originally published by CSDN.
  • 📢 author’s motto: life lies in tossing about, when you do not toss about life, life will start tossing you, let us work together! 💪 💪 💪

🎏 1. Difficulties in Windows and Linux communication in Jenkins

It is difficult to communicate with the Linux host in Windows. Even if you have installed the corresponding SSH and SCP tools, it is blocked because the password needs to be entered. In order to alleviate this trouble, I decided to develop a small tool to achieve seamless integration between Windows and Linux.

🎏 2. Extend command-line arguments

We use the CommandLineParser package to extend the parsing of command line arguments. It is relatively simple to use.

// Define a parameter class
public class Options
  {
      /// <summary>
      /// [email protected]:999
      /// </summary>
      [Value(0)]
      public string Host { get; set; }

      [Option('p', "password", Required = true, HelpText = "remote host password")]
      public string Password { get; set; }

      [Option('s', "source", Required = true, HelpText = "local windows dir path,include filename")]
      public string Source { get; set; }

      [Option('d', "dest", Required = true, HelpText = "remote host destination dir path")]
      public string Dest { get; set; }

      [Option('c', "cmd", Required = false, HelpText = "remote host shell script")]
      public string Cmd { get; set; }}}Copy the code

Using a Parser. Default. ParseArguments < Options > (args). WithParsed < Options > (o = > {}) parsing.

🎏 3. Ssh.net package

By encapsulating many protocols and ciphertext algorithms, SSH.net provides us with a good shell communication mechanism. Its package contains SFTP, which can be used to upload and download files.

Its usage is as follows:

using (var client = new SftpClient(connectionInfo)) { Console.WriteLine($"connecting {host} ..." ); client.Connect(); if (client.IsConnected) { } client.Disconnect(); }Copy the code

🎏 4, download and usage

Download address

4.1 sshClient 🎏

This tool runs mainly on Windows and sends files and runs shell commands to remote Linux hosts through SSH and SFTP.

The main purpose is to automate operation and maintenance. I run it in Jenkins to copy files from Windows Slave node to Linux, and then execute some specified scripts, such as decompression, etc. The main dependency is on the.net Core 3.1 runtime environment, and nothing else should be required.Copy the code

🎏 4.2, usage

SshClient 1.0.0 Copyright (C) 2021 sshClient

ERROR(S): Required option ‘p, password’ is missing. Required option ‘s, source’ is missing. Required option ‘d, dest’ is missing. Parameter meaning:

  • p, –password Required. remote host password

  • s, –source Required. local windows dir path,include filename

  • d, –dest Required. remote host destination dir path

  • c, –cmd optional , remote host shell script

  • -help Display this help screen.

  • -version Display version information.

Value pos. 0 remote host, eg: [email protected]:2200

🎏 4.3 examples

Use the following command line to run sshClient.

/sshClient [email protected] -p 150.com -s “D: \ work2021 \ sshClient \ sshClient \ bin \ Debug \ netcoreapp3.1 \ Microsoft Extensions. The Configuration. The DLL” – D/usr/dotnet – c “CD /usr/dotnet; mv Microsoft.Extensions.Configuration.dll sshclient-test.dll”

🎏 05. Summary

If you’re interested in seeing the code: go github.com/codeex/sshC…

Routine summary, rational view!

Knot is what, knot is I want you to like but can not get the loneliness. 😳 😳 😳

👓 all see this, still care to point a like?

👓 all points like, still care about a collection?

Do you care about a comment when you have collected 👓?