【问题标题】:Get ping replies with date & time获取带有日期和时间的 ping 回复
【发布时间】:2015-02-10 05:46:27
【问题描述】:

我想获得带有日期和时间的 ping 回复。我有这样的批处理脚本。

@echo off
ping -t 127.0.0.1|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 127.0.0.1>nul" >> pingTime.txt

在这里,我得到的是时间,而不是日期。我怎样才能得到这个?

谢谢

【问题讨论】:

    标签: ping


    【解决方案1】:

    你不是在问编程问题,但我在不久前写了这个,因为我需要它。

    using System;
    using System.IO;
    using System.Net.NetworkInformation;
    using System.Threading;
    
    class Program
    {
      static void Main(string[] args)
      {
        Console.WriteLine("Pinging {0} every 1 second", args[0]);
    
        string output = args.Length == 2 ? args[1] : null;
    
        if (output != null)
        {
          File.AppendAllText(output, 
            string.Format("Pinging {0} every 1 second{1}", 
              args[0], Environment.NewLine));
        }
    
        var p = new Ping();
    
        while (true)
        {
          var n = DateTime.Now;
          var r = p.Send(args[0], 1000);
          var e = DateTime.Now;
    
          string result = null;
    
          if (r.Status == IPStatus.TimedOut)
          {
            result = string.Format("{0:s} => timeout", n);
          }
          else
          {
            result = string.Format("{0:s} => {1}ms", n, r.RoundtripTime);
          }
    
          Console.WriteLine(result);
    
          if (output != null)
          {
            File.AppendAllText(output, result + Environment.NewLine);
          }
    
          Thread.Sleep((int) Math.Max(0, 1000 - (e - n).TotalMilliseconds));
        }
      }
    }
    

    将以上内容另存为tping.cs。用csc tping.cs编译。

    【讨论】:

    • 是否可以编辑我发布的上述批处理代码以显示日期?因为它已经显示时间。现在我需要和它约会
    【解决方案2】:

    这会起作用

    @echo off
    ping -t 127.0.0.1|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 127.0.0.1>nul" >> pingTime.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2012-03-11
      • 2014-11-20
      • 2017-12-01
      • 1970-01-01
      • 2017-04-13
      • 2018-02-07
      相关资源
      最近更新 更多