public static async Task DownloadGraphsAsync(string destDir, string baseUrl, string username, string password, IEnumerable <int> targetGraphIdList, CancellationToken cancel = default)
    {
        using var r = new CactiSession(baseUrl, username, password);

        List <CactiDownloadedGraph> list = new List <CactiDownloadedGraph>();

        foreach (int id in targetGraphIdList.Distinct())
        {
            await r.EnsureLoginAsync(cancel);

            Con.WriteLine($"Downloading id = {id} ...");

            try
            {
                var tmp = await r.DownloadGraphAsync(id, 1, 6, cancel);

                tmp._DoForEach(x => list.Add(x));
            }
            catch
            {
            }
        }

        if (list.Count == 0)
        {
            throw new CoresException($"Download cacti graphs failed.");
        }

        Con.WriteLine("Done.");

        foreach (var g in list)
        {
            string fn = destDir._CombinePath("graph_size_" + g.Size + "_id_" + g.GraphId.ToString("D5") + "_rra_" + g.RraId.ToString("D5") + ".png");

            Lfs.WriteDataToFile(fn, g.Data, FileFlags.AutoCreateDirectory);
        }
    }
Beispiel #2
0
        static int SslCertListCollector(ConsoleService c, string cmdName, string str)
        {
            ConsoleParam[] args =
            {
                new ConsoleParam("[dnsZonesDir]", ConsoleService.Prompt, "dnsZonesDir: ", ConsoleService.EvalNotEmpty, null),
                new ConsoleParam("OUT",           ConsoleService.Prompt, "outDir: ",      ConsoleService.EvalNotEmpty, null),
                new ConsoleParam("HASH"),
            };
            ConsoleParamValueList vl = c.ParseCommandList(cmdName, str, args);

            string dirZonesDir = vl.DefaultParam.StrValue;
            string outDir      = vl["OUT"].StrValue;
            string hashliststr = vl["HASH"].StrValue;

            string[] hashlist = hashliststr._Split(StringSplitOptions.RemoveEmptyEntries, ";", "/", ",", ":", " ");

            var dirList = Lfs.EnumDirectory(dirZonesDir, false);

            if (dirList.Where(x => x.IsFile && (IgnoreCaseTrim)Lfs.PathParser.GetExtension(x.Name) == ".dns").Any() == false)
            {
                // 指定されたディレクトリに *.dns ファイルが 1 つもない場合は子ディレクトリ名をソートして一番大きいものを選択する
                dirZonesDir = dirList.OrderByDescending(x => x.Name).Where(x => x.IsDirectory).Select(x => x.FullPath).First();
            }

            Con.WriteLine($"Target directory: '{dirZonesDir}'");

            // 1. DNS ゾーンファイルを入力してユニークな FQDN レコードの一覧を生成する
            DnsFlattenUtil flat = new DnsFlattenUtil();

            foreach (FileSystemEntity ent in Lfs.EnumDirectory(dirZonesDir, true))
            {
                if (ent.IsFile)
                {
                    string fn = ent.FullPath;
                    fn._Print();

                    flat.InputZoneFile(Lfs.PathParser.GetFileNameWithoutExtension(fn), Lfs.ReadDataFromFile(fn).Span);
                }
            }

            // 2. FQDN の一覧を入力して FQDN と IP アドレスのペアの一覧を生成する
            DnsIpPairGeneratorUtil pairGenerator = new DnsIpPairGeneratorUtil(100, flat.FqdnSet);

            List <SniHostnameIpAddressPair> list = pairGenerator.ExecuteAsync()._GetResult().ToList();

            // 3. FQDN と IP アドレスのペアの一覧を入力して SSL 証明書一覧を出力する
            SslCertCollectorUtil col = new SslCertCollectorUtil(1000, list);

            IReadOnlyList <SslCertCollectorItem> ret = col.ExecuteAsync()._GetResult();

            if (hashlist.Length >= 1)
            {
                List <SslCertCollectorItem> filtered = new List <SslCertCollectorItem>();
                ret.Where(x => hashlist.Where(y => y._IsSameHex(x.CertHashSha1)).Any())._DoForEach(x => filtered.Add(x));
                ret = filtered;
            }

            ret = ret.OrderBy(x => x.FriendName._NonNullTrim()._Split(StringSplitOptions.RemoveEmptyEntries, ".").Reverse()._Combine("."), StrComparer.IgnoreCaseTrimComparer).ToList();

            // 結果表示
            Con.WriteLine($"Results: {ret.Count} endpoints");

            // 結果保存
            string csv = ret._ObjectArrayToCsv(withHeader: true);

            XmlAndXsd xmlData = Util.GenerateXmlAndXsd(ret);

            string dir = outDir;

            Lfs.WriteDataToFile(Lfs.PathParser.Combine(dir, xmlData.XmlFileName), xmlData.XmlData, flags: FileFlags.AutoCreateDirectory);
            Lfs.WriteDataToFile(Lfs.PathParser.Combine(dir, xmlData.XsdFileName), xmlData.XsdData, flags: FileFlags.AutoCreateDirectory);
            Lfs.WriteStringToFile(Lfs.PathParser.Combine(dir, "csv.csv"), csv, flags: FileFlags.AutoCreateDirectory, writeBom: true);

            return(0);
        }