获取带有日期和时间的 ping 回复
Get ping replies with date & time
我想获得带有日期和时间的 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
在这里,我得到的是时间,但不是日期。我怎样才能得到这个?
谢谢
你问的不是编程问题,但我不久前写了这篇文章,因为我需要它。
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
.
编译
这会起作用
@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
我想获得带有日期和时间的 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
在这里,我得到的是时间,但不是日期。我怎样才能得到这个?
谢谢
你问的不是编程问题,但我不久前写了这篇文章,因为我需要它。
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
.
这会起作用
@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