1.

Solve : How to include timestamp on each line when piping to a file??

Answer»

Hello,

I have set up a ping and sending the output to a textfile like this:

ping www.microsoft.com > c:\pingtest.txt


What I need in addition is to have a timestamp on each line in the textfile so I can see at what times the network actually is down. I would appreciate help on how to do this.



Best regards,
Kjetil Johannesen
NorwayI'm on a Windows XP machine.

date /T > C:\pingtest.txt
time /T > C:\pingtest.txt
ping www.microsoft.com >C:\pingtest.txt

I don't know how to make it on a SINGLE line, WITHOUT other parsing.

Another solution:

echo %date% %time% >c:\pingtest.txt
ping www.microsoft.com >C:\pingtest.txt

I'm sorry, I don't know other solution. In fact I know, but involves using a programming language (C, Python, Java...)Thanks for responding - yes the trouble is really to unite the two commands.

A bit unclear in my first message I notice, what I really want is for the ping to stay up for a long period of time and have timestamp on each line so that my line today without timestamp looks like this:

ping www.microsoft.com -t > c:\pingtest.txtYou can make a bat, and put it to repeat from time to time... (scheduled tasks?) A few minutes interval between some pings it's ok?Thanks thats a good alternative - Id rather have it more often then once in a minute but its a good start.They will eat you alive It's like a Denial of Service attack based on ping
But, if your network is mission critical... And lifes depend on it... If you don't experience much disconnection, it's better to put it to run something like:
echo %date% %time% >c:\pingtest.txt
ping -n 250 www.microsoft.com >c:\pingtest.txt

Run it at few minutes (ping -n 250 : repeat the ping for 250 times... And this summs up).

Be careful though... It could be intercepted as an Denial of Service attack by your ISP. You don't leave PING working for half an hour, except for important (very important) cases... But maybe I am wrong. Better ask your ISP if they don't have a problem with your idea.Another solution (a SMALL python script made by JE ):
import os
import sys
import time
counting=100
f=file(r'c:\pingtest.txt','a')
try:
#launch: python pinging.py f_times, where f_times is a integer biger than 0
if int(sys.argv[1])>0:
counting=int(sys.argv[1])
except:
#If no legit argument is given, then counting=100
pass

#repeat what's next for "counting" times
for x in range(counting):
#the command to execute: ping -n site
pingData=os.popen('ping -n 1 www.microsoft.com')
#note the date and time in c:\pingtest.txt
f.write("*"*50+'\n')
f.write(" ".join(str(l) for l in time.localtime()[:6]))
#write the ping result in c:\pingtest.txt
for x in pingData.readlines():
f.write(x)
f.write('\n')
#wait 5 seconds
time.sleep(5)

f.close()


How to call it:
python pingtest.py 100
(it will run 100 pings, at 5 second interval)
The date will be a line like: year (4 digits) [space] month (1-2digits) [space] date (1-2 digits) [space] hour min seconds (with spaces between)
Something like: 2007 3 30 18 50 40
Next lines are the ping response



Discussion

No Comment Found