Solved

Batch file with ping

Posted on 2004-04-16
7
1,350 Views
Last Modified: 2012-06-27
@echo off
set /a errors=0
echo Trying to ping host %1
For /f "tokens=1" %%s in ('ping "-n" "1" %1') DO (
  if "%%s"=="Svar" (
    echo --Success--
    goto END
  )
)

echo !!Failed!!
:END

This code in my bat file returns whether a ping was sucessful or failed. ("Svar" is the swedish version of "Response" as in "Response received from xxx.xxx.xxx.xxx")

How do I add to the above code to make it show the time it too to receive the response when the ping is succesful?

Regards,
MHQ
0
Comment
Question by:MHQ
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:mrdtn
Comment Utility
Simple.  Use %date% and %time% as shown below.

@echo off
set /a errors=0
echo Trying to ping host %1
For /f "tokens=1" %%s in ('ping "-n" "1" %1') DO (
  if "%%s"=="Svar" (
    echo --Success-- at %date% %time%
   goto END
 )
)

echo !!Failed!!
:END
0
 
LVL 3

Author Comment

by:MHQ
Comment Utility
I'm sorry, I missed a "k" in my post.. though you should have been ablse to see what I ment anyhow.. I want to display the time it tooK to receive the responce.. that is, the time in ms reported by the ping. Sorry for the confusion
0
 
LVL 6

Accepted Solution

by:
mrdtn earned 100 total points
Comment Utility
Extract token 5 as well.  It will show up as replacebale param %%t.  Let me know if this is what you were looking for.

--

mrdtn

--

@echo off
set /a errors=0
echo Trying to ping host %1
For /f "tokens=1,5" %%s in ('ping "-n" "1" %1') DO (
  if "%%s"=="Svar" (
    echo --Success-- %%t
    goto END
  )
)

echo !!Failed!!
:END
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Author Comment

by:MHQ
Comment Utility
That worked perfectly! But... how come it is stored in %%t ? What is that based on?
0
 
LVL 6

Expert Comment

by:mrdtn
Comment Utility
That's just the way it works.  Open a command window and type "for /?" for a long list of information.

Basically, when you tell the for statement to get more than one token, it assigns them one by one in sequence beginning with the letter of the variable you specified in the statement, so for example:

for /f "tokens=1-5,*" %%a . . .

will assign tokens to %%a, %%b, %%c, %%d, %%e . . .

The "*" tells the statement to assign anything that might be left over to yet a 6th token, which will be %%f.

Therefore, if you have a lot to parse, best not to start with x, y, or z.

--

mrdtn
0
 
LVL 3

Author Comment

by:MHQ
Comment Utility
Ic, didn't notice that "t" is after "s".. makes sence =) thanx alot!
0
 
LVL 6

Expert Comment

by:mrdtn
Comment Utility
It's subtle . . . but then if it weren't, it wouldn't be so much fun!!

Cheers,

mrdtn
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now