我最后在Python中为此编写了一个快速的小脚本,但我想知道是否有一个实用程序可以提供文本,其中每个行前面都有一些文本 - 在我的特定情况下,是一个时间戳.理想情况下,使用方式如下:
cat somefile.txt | prepend-timestamp
Run Code Online (Sandbox Code Playgroud)
(在你回答sed之前,我试过这个:
cat somefile.txt | sed "s/^/`date`/"
Run Code Online (Sandbox Code Playgroud)
但是,这只会在执行sed时评估date命令,因此每行都会错误地添加相同的时间戳.)
Ping默认返回此值:
64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms
Run Code Online (Sandbox Code Playgroud)
有什么方法可以让它添加时间戳吗?
例如,
Mon 21 May 2012 15:15:37 EST | 64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms
Run Code Online (Sandbox Code Playgroud)
我在OS X v10.7(Lion)上似乎有一些BSD版本的ping.
我需要在 Windows 中 ping 东西,并知道什么时候收到或错过了 ping;时间戳应反映 24 小时制(而不是 AM/PM 后缀)。
如何在 Powershell 中执行此操作?
我最近有很多互联网连接停机时间.因此,我想列出每次遇到问题时将其发送给我的ISP.为了使它不那么乏味,我想,制作一种报告并将其作为日志文件发送会很好.我偶然发现了一些解决方案,但我找到的最好的解决方案是在这个网站上:用Joost Kuin的时间戳 Ping.
我已经调整了一下这个代码,但我遇到了一个问题.如果我尝试使用if not %errorlevel% 0 do (...),if %errorlevel% 1 do (...)并尝试运行.bat文件,Windows命令处理器窗口会弹出并消失.没有这个条件,批处理文件运行正常.但它没有做它的目的.我想制作2个文件,一个使用普通的ping日志,另一个只有超时和目标主机无法访问的时间戳.这是我第一次使用Windows脚本.我以前习惯用C++编写代码.我没有获得host_ip,而是获得了与外部网络的第一次连接的IP地址.
@echo off
set host=host_ip
set logfile=Ping_test.log
set logfile_fail=Ping_test_fail.log
echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1') do (echo (%%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
if not %errolevel% 0 do (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% errorlvel %errorlevel% %%A>>%logfile_fail%)
echo %errorlevel% …Run Code Online (Sandbox Code Playgroud) awk ×1
bash ×1
batch-file ×1
bsd ×1
osx-lion ×1
ping ×1
powershell ×1
shell ×1
unix ×1
windows ×1