windows环境下的Jenkins必备工具——向linux上传文件执行shell的小工具

这是我参与8月更文挑战的第15天,活动详情查看:8月更文挑战

  • ?欢迎点赞 :? 收藏 ⭐留言 ? 如有错误敬请指正,赐人玫瑰,手留余香!
  • ?本文作者:由webmote 原创,首发于 【CSDN】
  • ?作者格言: 生活在于折腾,当你不折腾生活时,生活就开始折腾你,让我们一起加油!???

? 1.jenkins中windows和linux通讯的困难

在windows下想和Linux主机进行通信,是比较困难的,就算安装了相应的ssh、scp工具,也因为密码需要输入的缘故而被阻挡住,为了减轻这个麻烦,我决定开发一个小工具,使得在windows和linux间做到无缝集成。

? 2、扩展命令行参数

我们使用 CommandLineParser包来扩充对命令行参数的解析。
其使用相对简单。

//定义一个参数类
public class Options
  {
      /// <summary>
      /// root@111.222.1.1: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; }

  }
}
复制代码

使用 Parser.Default.ParseArguments<Options>(args) .WithParsed<Options>(o =>{})解析即可。

? 3、ssh.net 包

通过封装诸多协议和密文算法,ssh.net 给我们提供了良好的shell通信机制,其包内封装了sftp可以用来上传下载文件。

其用法如下:

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

   }
复制代码

? 4、下载和用法

下载地址

? 4.1 sshClient

这款工具主要运行在Windows上,通过ssh和sftp 向远程linux主机发送文件和运行shell命令。

      主要用途应该是自动化运维,我是在Jenkins中运行它,以便把windows slave节点的文件拷贝到Linux中,然后再执行一些指定的脚本,比如解压等。
      
      其主要依赖是.net core 3.1运行环境,其他的应该均不需要。
复制代码

?4.2、用法

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.
参数意义:

  • 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: root@192.168.0.1:2200

? 4.3、例子

采用以下命令行来运行sshClient。

./sshClient root@192.168.0.150 -p 150.com -s “D:\work2021\sshClient\sshClient\bin\Debug\netcoreapp3.1\Microsoft.Extensions.Configuration.dll” -d /usr/dotnet -c “cd /usr/dotnet;mv Microsoft.Extensions.Configuration.dll sshclient-test.dll”

? 05. 小结

如果有兴趣想看看代码:go github.com/codeex/sshC…

例行小结,理性看待!

结的是啥啊,结的是我想你点赞而不可得的寂寞。???

?都看到这了,还在乎点个赞吗?

?都点赞了,还在乎一个收藏吗?

?都收藏了,还在乎一个评论吗?

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享