Example #1
0
 static void RunServer(ServerTask server)
 {
     Console.WriteLine("开始连接服务器:" + server.host);
     using (var sshClient = new SshClient(server.host, server.port, server.username, server.password))
     {
         sshClient.Connect();
         Console.WriteLine("服务器{0}连接成功!", server.host);
         foreach (var command in server.commands)
         {
             Console.WriteLine("开始执行命令:" + command);
             var arr = command.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
             if (arr.Length == 3 && arr[0].ToLower() == "upload")
             {
                 Upload(server, arr[1], arr[2]);
             }
             else if (arr.Length == 3 && arr[0].ToLower().StartsWith("uploaddir"))
             {
                 var s = arr[0].Substring(9);
                 UploadDir(server, arr[1], arr[2], string.IsNullOrEmpty(s) ? 0 : int.Parse(s));
             }
             else if (arr.Length == 2 && arr[0].ToLower() == "bak")
             {
                 Bak(sshClient, arr[1]);
             }
             else if (arr.Length >= 3 && arr[0].ToLower() == "zipbak")
             {
                 ZipBak(sshClient, arr[1], arr.Skip(2));
             }
             else
             {
                 ExcuteCommand(sshClient, command);
             }
         }
     }
 }
Example #2
0
 static void Upload(ServerTask server, string sourcePath, string targetPath)
 {
     Console.WriteLine("开始上传文件 {0} 到 {1}", sourcePath, targetPath);
     using (var sftpClient = new SftpClient(server.host, server.port, server.username, server.password))
     {
         sftpClient.Connect();
         var   fs    = File.Open(sourcePath, FileMode.Open);
         ulong total = (ulong)fs.Length;
         sftpClient.UploadFile(fs, targetPath, current =>
         {
             Console.Write("上传进度: {0}/{1}----------{2}%\r", current, total, current * 100 / total);
         });
     }
     Console.WriteLine("\n文件上传完成");
 }
Example #3
0
        static List <ServerTask> LoadTasks(Config config)
        {
            List <ServerTask> tasks = new List <ServerTask>();

            foreach (var s in config.servers)
            {
                var packages = s.packages.Where(p => p.enable).OrderBy(p => p.order).ToArray();
                if (packages.Length > 0)
                {
                    ServerTask t = new ServerTask();
                    t.host     = s.host;
                    t.port     = s.port;
                    t.username = s.username;
                    t.password = s.password;
                    t.commands = new List <string>();
                    foreach (var package in packages)
                    {
                        var variables = package.variables;
                        if (variables != null)
                        {
                            variables = variables.Select(p =>
                            {
                                if (p.StartsWith(GLOBAL))
                                {
                                    return(config.globals[int.Parse(p.Substring(6))]);
                                }
                                return(p);
                            }).ToArray();
                        }
                        foreach (var c in package.commands)
                        {
                            string command = c;
                            if (variables != null)
                            {
                                command = Regex.Replace(c, @"\{\d+\}", m =>
                                {
                                    int i = int.Parse(m.Value.Trim('{').Trim('}'));
                                    return(variables[i]);
                                });
                            }
                            t.commands.Add(command);
                        }
                    }
                    tasks.Add(t);
                }
            }
            return(tasks);
        }
Example #4
0
        static void UploadDir(ServerTask server, string sourceDir, string targetDir, int count)
        {
            Console.WriteLine("开始上传文件夹 {0} 到 {1}", sourceDir, targetDir);
            DirectoryInfo di = new DirectoryInfo(sourceDir);

            if (!di.Exists)
            {
                throw new Exception($"文件夹{sourceDir}不存在");
            }
            IEnumerable <FileInfo> files = di.GetFiles();

            if (count > 0)
            {
                files = files.OrderByDescending(p => p.LastWriteTime).Take(count);
            }
            foreach (var file in files)
            {
                Upload(server, file.FullName, Path.Combine(targetDir, file.Name));
            }
        }