{"id":66663,"date":"2006-08-17T07:23:00","date_gmt":"2006-08-17T07:23:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/17\/how-can-i-modify-the-output-of-the-ping-command\/"},"modified":"2006-08-17T07:23:00","modified_gmt":"2006-08-17T07:23:00","slug":"how-can-i-modify-the-output-of-the-ping-command","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-modify-the-output-of-the-ping-command\/","title":{"rendered":"How Can I Modify the Output of the Ping Command?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I place the current date and time at the beginning of each line of Ping.exe output?<BR><BR>&#8212; AL<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, AL. We have to admit, we were a little shocked by your question. Change the output of the Ping command? Really, AL. Would you ask us to rewrite <I>Romeo and Juliet<\/I>, giving it a more happy and upbeat ending? Would you ask us to fix up the <I>Mona Lisa<\/I>, giving Mona blonde hair and maybe a cute little butterfly tattoo? Would you ask us to take the peanuts out of peanut M&amp;M\u2019s and replace them with soybeans? Some things should not be tampered with; they\u2019re perfectly fine just the way they are.<\/P>\n<P>But, to tell you the truth, we don\u2019t consider Ping.exe to be one of those things:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;WScript.Shell&#8221;)<\/p>\n<p>Set objWshScriptExec = objShell.Exec(&#8220;ping 192.168.1.1&#8243;)<\/p>\n<p>Set objStdOut = objWshScriptExec.StdOut<\/p>\n<p>Do Until objStdOut.AtEndOfStream\n    strLine = objStdOut.ReadLine\n    If Len(strLine) &gt; 2 Then\n        WScript.Echo Now &amp; &#8221; &#8212; &#8221; &amp; strLine\n    Else\n        Wscript.Echo strLine\n    End If\nLoop\n<\/PRE>\n<P>So what are we doing here? Well, to begin with, we\u2019re creating an instance of the <B>Wscript.Shell<\/B> object. After that, we call the Shell object\u2019s <B>Exec<\/B> method, asking Exec to run the command-line command <B>ping 192.168.1.1<\/B>:<\/P><PRE class=\"codeSample\">Set objWshScriptExec = objShell.Exec(&#8220;ping 192.168.1.1&#8221;)\n<\/PRE>\n<P>If you aren\u2019t familiar with the Exec method (which was introduced as part of Windows Script Host version 5.6) it\u2019s actually a nifty little command. Exec is similar to the <B>Run<\/B> method; the primary difference is that while the Run method runs a command-line tool in a visible command window, the Exec method runs that same command-line tool in a hidden window. When you call Ping.exe using Exec, you\u2019ll never see Ping \u2013 or its output \u2013 onscreen.<\/P>\n<P>That\u2019s a good point: Ping isn\u2019t really all that useful if you never see the results, is it? But that\u2019s OK: we can still get at the output. That\u2019s because a command-line tool run using Exec has all its output stored in the <B>StdOut<\/B> property of the <B>WSHScriptExec<\/B> object (an object we created at the same time we called the Exec method). To get at the output of Ping.exe we simply need to read the value of the StdOut property.<\/P>\n<P>So let\u2019s see how we go about doing that. To begin with, here\u2019s a line of code that binds us to the StdOut property:<\/P><PRE class=\"codeSample\">Set objStdOut = objWshScriptExec.StdOut\n<\/PRE>\n<P>After we\u2019ve made the connection we set up a Do Until loop that reads StdOut line-by-line until there are no more lines to read (that is, until the <B>AtEndOfStream<\/B> property is true). Admittedly, we don\u2019t need to read StdOut line-by-line; we\u2019re only doing it in this case because we want to put the current date and time at the beginning of each line. <\/P>\n<TABLE id=\"EEE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Admittedly, we\u2019re cheating a tiny bit: we\u2019re actually calling Ping, waiting for the command to finish, and then \u2013 slightly after the fact \u2013 adding the date and time to the beginning of each line. That means our times will always be a few seconds off. However, that\u2019s about the best we can do: we can\u2019t ask Ping to issue the first ping, wait for us to grab the results and tack on the date and time, and only <I>then<\/I> run the second ping. If you feel the need to get that level of accuracy \u2013 and if you\u2019re running Windows XP or Windows Server 2003 \u2013 then you might want to use the WMI class <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0914.mspx\"><B>Win32_PingStatus<\/B><\/A> and, in essence, create your own, customized version of Ping.exe.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Inside the loop we use this line of code to read the first line from StdOut:<\/P><PRE class=\"codeSample\">strLine = objStdOut.ReadLine\n<\/PRE>\n<P>We then use the <B>Len<\/B> function to determine whether or not the line has more than two characters in it. Why? Well, Ping\u2019s output typically includes a few blank lines; because we don\u2019t want to stick the current date and time on a blank line we use a little bit of programming intelligence to weed out blank lines. So then why do we check for lines greater than <I>2<\/I> characters? Shouldn\u2019t we check for lines that have 0 characters? <\/P>\n<P>That would seem to make sense. However, in command-line output a blank line doesn\u2019t really have 0 characters; instead a blank line actually has two characters: a carriage return and a linefeed. If we checked for lines that had a length equal to 0 characters we\u2019d never find one.<\/P>\n<P>Suppose a line <I>does<\/I> have more than two characters in it. In that case we echo back the current date and time (using the VBScript function <B>Now<\/B>) followed by the line of code we read in from StdOut:<\/P><PRE class=\"codeSample\">WScript.Echo Now &amp; &#8221; &#8212; &#8221; &amp; strLine\n<\/PRE>\n<P>If the line <I>doesn\u2019t<\/I> have more than two characters then it must be a blank line. In that case, we don\u2019t attach the current date and time but instead echo back just the value read in from StdOut:<\/P><PRE class=\"codeSample\">Wscript.Echo strLine\n<\/PRE>\n<P>Will that do the trick? Judge for yourself:<\/P><PRE class=\"codeSample\">8\/6\/2006 2:42:45 PM &#8212; Pinging 192.168.1.1 with 32 bytes of data:<\/p>\n<p>8\/6\/2006 2:42:45 PM &#8212; Reply from 192.168.1.1: bytes=32 time&lt;1ms TTL=128\n8\/6\/2006 2:42:46 PM &#8212; Reply from 192.168.1.1: bytes=32 time&lt;1ms TTL=128\n8\/6\/2006 2:42:47 PM &#8212; Reply from 192.168.1.1: bytes=32 time&lt;1ms TTL=128\n8\/6\/2006 2:42:48 PM &#8212; Reply from 192.168.1.1: bytes=32 time&lt;1ms TTL=128<\/p>\n<p>8\/6\/2006 2:42:48 PM &#8212; Ping statistics for 192.168.1.1:\n8\/6\/2006 2:42:48 PM &#8212;     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\n8\/6\/2006 2:42:48 PM &#8212; Approximate round trip times in milli-seconds:\n8\/6\/2006 2:42:48 PM &#8212;     Minimum = 0ms, Maximum = 0ms, Average = 0ms\n<\/PRE>\n<TABLE id=\"EUF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Incidentally, many years ago the legendary Dean Tsaltas wrote a <I>Tales From the Script<\/I> column that, among other things, used this same approach to show how you can modify the output you get back from command-line tools. If that sounds interesting to you <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg1002.mspx\"><B>check it out<\/B><\/A>. (This, by the way, was the very first column published in the Script Center. And it remains as useful today as it was back in 2002!)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We hope that helps, AL. As for putting a butterfly tattoo on the Mona Lisa, though, well, we\u2019re afraid we can\u2019t help you there:<\/P><IMG border=\"0\" alt=\"Tattoo\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/mona.jpg\" width=\"279\" height=\"168\"><BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I place the current date and time at the beginning of each line of Ping.exe output?&#8212; AL Hey, AL. We have to admit, we were a little shocked by your question. Change the output of the Ping command? Really, AL. Would you ask us to rewrite Romeo and Juliet, giving [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,25,37,2,3,4,5],"class_list":["post-66663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-displaying-output","tag-networking","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I place the current date and time at the beginning of each line of Ping.exe output?&#8212; AL Hey, AL. We have to admit, we were a little shocked by your question. Change the output of the Ping command? Really, AL. Would you ask us to rewrite Romeo and Juliet, giving [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66663\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}