Solved

Creation of a Basic batch file tp generate ping output

Posted on 2007-03-20
15
2,201 Views
Last Modified: 2013-12-04
I need to create a batch file that will ping in excess of 25 IP addresses and generate output to a .txt file.  I'm sure this is a nice easy one for someboy to answer.  I have already tried running the command below but am prompted with the message C:\Computers.txt was unexpected at this time.  I'm sure this is something very simple..

For /f %a in C:\Computers.txt do ping %a -n 2 >> C:\Ping_Results.txt
0
Comment
Question by:OGLED
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 10

Expert Comment

by:Phadke_hemant
ID: 18759763
use %i instead of %a
0
 

Author Comment

by:OGLED
ID: 18759807
Have tried %i instead of %a but the same error message occurs (C:\Computers.txt was unexpected at this time).  

Any other suggestions?
0
 
LVL 10

Expert Comment

by:Phadke_hemant
ID: 18759816
try this one -
for /f %i in (C:\Computers.txt ) do ping %i >> C:\Ping_Results.txt
0
 

Author Comment

by:OGLED
ID: 18759860
I am now prompted with the message:  The System cannot find the file C:\Computers.txt.  
Obviously I am 100% sure that c:\computers.txt exists.
0
 
LVL 10

Expert Comment

by:Phadke_hemant
ID: 18759882
please verify the filename (spelling) again
0
 
LVL 10

Expert Comment

by:Phadke_hemant
ID: 18759895
also check that you have not checked "Hide extensions for known file types" in folder options - view
sometimes your filename becomes computers.txt.txt
please verify this also
0
 

Author Comment

by:OGLED
ID: 18760047
Ok were making progress, thanks.  The file extensions were not displayed, schoolboy error on my part.

My overall requirement is as follows:
We have 25 network devices that we want to ping every 15 minutes so we can measure the response in milliseconds over a 24hr period.  I suspect the best way to do this is to create a scheduled task that calls the ping batch file every 15 minutes.  I want each ping to append the data to the same log file and provide a date / time stamp so our network guys can do some analysis around why the response is slower at specific intervals in the day.  

Obviously the desired solution would be to implement qos so we can prioritise network traffic / allocate bandwidth utilisation but we are some way from achieving this.  We are also in the process of implementing HP's NNM (network node manager) which would also asist with this type of troubleshooting.
0
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.

 
LVL 10

Expert Comment

by:Phadke_hemant
ID: 18760127
yes use scheduled task to run a script to run this command and to append the result file so that you will not loose previous result files
0
 

Author Comment

by:OGLED
ID: 18760143
The C:\Ping_Results.txt file does not contain a date / time stamp for when the command was ran?  
Any ideas? Sorry to be a pain but need to get this working tonight if possible.
0
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 500 total points
ID: 18762022
First %i or %a doesn't matter.  either can be used - that's not the problem.  You're running this in a batch file, right?  Well, in a batch file, when using the for command, you need to double the % signs.  In addition you need to put the file name in parens.

For /f %%a in (C:\Computers.txt) do ping %%a -n 2 >> C:\Ping_Results.txt
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 18762024
So to reiterate:

In a batch file:
%%a

As a single command on the command line:
%a
0
 
LVL 1

Expert Comment

by:SysAdmin_Doug
ID: 18764761
To add the date and time is not difficult.  

Try:

time /t
This will give the time.

date /t  -- this will give the date.  

Add those commands to run before your ping in the batch file (with the output piped to your file) and you should be all set.
0
 
LVL 3

Expert Comment

by:adnix
ID: 18764918
I suggest that to log the date/time, just change the dos prompt to display the date and time.
This way, you will see the updated date/time on every line.

eg. add this line to the first line in your batch file
prompt $d $t

try it and see the result! :)
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 18765593
Actually, it's easier still - you can use %date% and %time% to display the date and time:


For /f %%a in (C:\Computers.txt) do (
   echo %date% %time%>>c:\Ping_Results.txt
   ping %%a -n 2 >> C:\Ping_Results.txt
)
0
 
LVL 10

Expert Comment

by:Phadke_hemant
ID: 18765880
therefore your batch looks like -

date /t >> C:\Ping_Results.txt
time /t >> C:\Ping_Results.txt
For /f %%i in (C:\Computers.txt) do ping %%i -n 2 >> C:\Ping_Results.txt
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

20 Experts available now in Live!

Get 1:1 Help Now