一、安装
-
"SSH.NET" Version="2020.0.2" />
二、新建.netcore 项目
Program.cs
- // See https://aka.ms/new-console-template for more information
-
-
- using Renci.SshNet;
-
- var connectionInfo = new ConnectionInfo("192.168.31.132",
- "root",
- new PasswordAuthenticationMethod("root", "linux服务器密码"),
- new PrivateKeyAuthenticationMethod("rsa.key"));
- using (var client = new SftpClient(connectionInfo))
- {
- client.Connect();
- var packagesRootPath = "/data/baget-data/packages/packages";
- var packageDirs = client.ListDirectory(packagesRootPath);
- var needRemovePackPaths = new List<string> { };
- foreach (var packagesDir in packageDirs)
- {
- if (packagesDir.Name == "." || packagesDir.Name == "..") continue;
- var subPackagePath = $"{packagesRootPath}/{packagesDir.Name}";
- var subPackages = client.ListDirectory(subPackagePath);
- foreach (var subPackage in subPackages)
- {
- if (subPackage.Name == "." || subPackage.Name == "..") continue;
- if (subPackage.FullName != subPackages.Last().FullName)
- {
- needRemovePackPaths.Add(subPackage.FullName);
- }
- else
- {
- Console.WriteLine($"保留包:{subPackage.FullName}");
- }
- Console.WriteLine(subPackage.FullName);
- }
- }
- foreach (var needRemovePackPath in needRemovePackPaths)
- {
- var subFiles = client.ListDirectory(needRemovePackPath);
- foreach (var subFile in subFiles)
- {
- if (subFile.Name == "." || subFile.Name == "..") continue;
- client.DeleteFile(subFile.FullName);
- }
- client.DeleteDirectory(needRemovePackPath);
- Console.WriteLine($"成功删除包:{needRemovePackPath}");
- }
- }
-
- Console.WriteLine("清理包完成!");