コード例 #1
0
        static public bool Logging(Datas.PingData data)
        {
            IPHostEntry iPHost = Dns.GetHostEntry(data.Reply.Address);

            switch (data.Reply.Status)
            {
            case IPStatus.Success: logger.logState($"SUCCEED TO:{data.iPHostEntry.HostName} TIME_AVE:{data.AveragePingTime} ms", Logger.status.Connect); break;

            case IPStatus.TimedOut: logger.logState($"TIMEOUT TO:{data.iPHostEntry.HostName} TIME_AVE:{data.AveragePingTime} ms", Logger.status.DisConnect); break;

            case IPStatus.BadDestination: logger.logState($"BADROUTE TO:{data.iPHostEntry.HostName} TIME_AVE:{data.AveragePingTime } ms", Logger.status.DisConnect); break;

            case IPStatus.Unknown: logger.logState($"UNKNOWN TO:{data.iPHostEntry.HostName} TIME_AVE:{data.AveragePingTime} ms", Logger.status.DisConnect); break;

            default: logger.logState($"SOMEREASON TO{data.iPHostEntry.HostName} TIME_AVE:{data.AveragePingTime} ms", Logger.status.DisConnect); break;
            }
            return(data.Reply.Status == IPStatus.Success);
        }
コード例 #2
0
        /// <summary>
        /// 経路情報を調べ上げる
        /// </summary>
        /// <param name="destAddress">目的アドレス</param>
        /// <param name="pingCount">ping回数</param>
        /// <param name="timeOut">タイムアウト時間</param>
        /// <param name="maxHop">TTL,調べるノードの最大数</param>
        /// <returns></returns>
        static public List <Datas.TraceData> TraceRoute(IPAddress destAddress, int pingCount, int timeOut, int maxHop = 32)
        {
            var traceData = new List <Datas.TraceData>();

            for (int cnt = 0; cnt < maxHop; cnt++)
            {
                traceData.Add(new Datas.TraceData());
                Datas.PingData data = DoPing(destAddress, timeOut, pingCount, maxHop);
                traceData[cnt].address = destAddress;
                traceData[cnt].reply   = data.Reply;
                if (data.Reply.Status != IPStatus.Success)
                {
                    break;                                       //pingが通らなかったら切断として認識する。
                }
                if (data.Reply.Address == destAddress)
                {
                    break;                                   //アドレスが目的地アドレスだったら終了
                }
            }
            return(traceData);
        }
コード例 #3
0
        /// <summary>
        /// ping送信関数
        /// </summary>
        /// <param name="address">アドレス</param>
        /// <param name="timeout">タイムアウト時間</param>
        /// <param name="count">ping送信回数</param>
        /// <returns>統計データ</returns>
        static public Datas.PingData DoPing(IPAddress address, int timeout, int count, int maxHop = 32)
        {
            var dReply  = new Datas.PingData();
            var pOption = new PingOptions();

            pOption.Ttl = maxHop;
            using (Ping ping = new Ping())
            {
                byte[] buffer    = new byte[32];
                long   TotalTime = new long();
                for (int cnt = 0; cnt < count; cnt++)
                {
                    var reply = ping.Send(address, timeout);
                    if (reply.Status == IPStatus.Success)
                    {
                        TotalTime   += reply.RoundtripTime;
                        dReply.Reply = reply;
                    }
                }
                dReply.AveragePingTime = TotalTime / count;
            }
            return(dReply);
        }
コード例 #4
0
        static int Main(string[] args)
        {
            Console.WriteLine($"Read Config File at {configstr}");
            //コンフィグファイルのチェック
            if (!FileCheck(configstr))
            {
                config          = new Datas.Config();
                config.Adresses = new System.Collections.Generic.List <string>();
                config.Adresses.Add("");
                config.Span        = new TimeSpan(0, 0, 45);
                config.LogFilePath = "/var/log/netStatus/net.log";
                string cfg = xml <Datas.Config> .SerializeXml(config);

                using (TextWriter writer = new StreamWriter(configstr))
                {
                    writer.Write(cfg);
                    writer.Flush();
                    writer.Close();
                }
            }
            else
            {
                using (StreamReader sr = new StreamReader(configstr))
                {
                    config = xml <Datas.Config> .ReadXml(sr.BaseStream);
                }
            }
            logger = new Logger(config.LogFilePath);
            //メイン関数
            while (true)
            {
                try
                {
                    foreach (var addressStr in config.Adresses)
                    {
                        IPAddress      address = IPAddress.Any;
                        Datas.PingData data    = new Datas.PingData();
                        //セーブ記載のデータがIPアドレスでなかった(ドメイン)だった時の処理
                        if (IPAddress.TryParse(addressStr, out address) == false)
                        {
                            IPHostEntry iPHost = Dns.GetHostEntry(address);
                            address          = iPHost.AddressList[0];
                            data.iPHostEntry = iPHost;
                        }
                        data = network.DoPing(address, timeout, pingCnt);
                        if (!Logging(data))
                        {
                            break;
                        }
                        //以下到達できなかった時のトレースコード
                        var traceData = network.TraceRoute(address, 2, 2048);
                        int Routes    = 0;
                        logger.writeText("TracePahse...");
                        foreach (var traceDetum in traceData)
                        {
                            logger.writeText($"[{Routes}]: {traceDetum.reply.Address} / {traceDetum.reply.RoundtripTime} ms | {traceDetum.reply.Status.ToString()}");
                        }
                    }
                    System.Threading.Thread.Sleep(config.Span);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }