• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Batch file to ping network

Yin

Joined
Feb 19, 2006
Messages
540 (0.08/day)
Processor Intel i7 920
Motherboard ASUS RAMPAGE II Extreme
Cooling CM v10
Memory Corsair 3 x 2GB TR3X6G1866C9DF
Video Card(s) Nvidia XFX 8800GT Stock
Storage Seagate ST3500320AS, 2 x Samsung HD103UJ
Display(s) 2408WFP
Case Corsair 800D
Power Supply Corsair HX750W
Hi Guys,

I need some help writing a batch file to test network connectivity for various locations.
Basically we are transitioning to from an existing network provider to a new network provider.

What I would like the script to do

Ping a list of IP address and then output the results into a simple text file of each IP Address.

The text file ideally should just display
something along the lines of

ping www.google.com Successful

or

ping www.google.com Unsuccessful

This is what I have so far but it is a bit broken.
Code:
@echo off
Title Cut over to Telstra Check

set OutputFile=result.txt
cd\
CLS

Echo. Cut over test
Echo. 
Echo. Ping all sites
Pause
Ping www.google.com.au | find "Reply"
	echo ping to google successful >>"C:\nettest\OutputFile.txt"

	IF NOT echo ping to google unsuccessful >>"C:\nettest\OutputFile.txt"

Ping www.techpowerup.com | find "Reply"
	echo ping to TechPowerup successful >>"C:\nettest\OutputFile.txt"

	IF NOT echo ping to Techpowerup unsuccessful >>"C:\nettest\OutputFile.txt"
pause

I'm not sure how to do a IF NOT command and make it jump to the next line.

Thanks!
 
I don't know much about programming, but what about an "Else" statement?

This guy knows more: Batch files branching
 
haha we are on the same boat, I'm clueless when it comes to programming.

I'll give it a go =)
 
Thats a good idea however could we make the text file name indicate if it was successful or not or is that the complicated bit?

Also on another note would it be easier to do this in vb script?
 
No, batch files are extremely easy.

A "If" requires three extra steps.

Code is not how much you can put in, but how lite can you make it and still perform the functions.

So

@ echo off


ping 1.4.1.4 >a.txt

findstr /m "Request timed out" a.txt
if %errorlevel%%==0 (
echo Save our nets!!!
)

pause
 
You need to use GOTO :toast:

EDIT: I would write an example.. but I'm off to bed. If you are still having problems tomorrow. I'll chime in. :toast: Steevo is on the right track! :toast:
 
Okay... getting confused, Sorry I'm a noob programmer.

I've copied Steevo code and tried to modify it to suit my requirements and it isn't working. I presume I'm suppose to use the goto command

Code:
@echo off

echo testing 1.4.1.4
ping 1.4.1.4 >a.txt

findstr /m "Request timed out" a.txt
if %errorlevel%%==0 (
echo Save our nets!!!
)

echo testing google
ping www.google.com.au >google.txt

findstr /m "Request timed out" google.txt
if %errorlevel%%==0 (
echo Save our nets!!!
)

findstr /m "Reply from" google.txt
if %errorlevel%%==0 (
echo yay google working!!!

pause

I understand that the idea is to make it as light as possible but the reason why I'm trying to make it as automated as possible is because we plan on level 1 people to test network connectivity at a few different sites as we are short on resources and we are required to do all of this on a Saturday morning (my usual catch up on sleep time).

I just realized i'm going to need to make the bat files create text files.

edit okay simple fix would be to put del *.txt which would delete all text files and pretty much refresh it all the time at the beginning.
 
Last edited:
Just reuse the same name and it will overwrite the files each time the batch is run.
 
you would be better off writing something in c++ or even VB
batch file != programming
batch files are extremely limited
 
You could install Cygwin and use BASH which is much more capable of doing what you're describing. I've actually written a script that does something like this using Ruby, where it takes in a list of IPs and returns a list of connected or disconnected users in either easy to read or XML format. Maybe something like that is what you're looking for? This is unmodified but might suit your purposes on a POSIX machine with BASH and Ping.

Code:
#!/usr/bin/ruby1.9.1
require "nokogiri"

ipfile = File.open('/etc/openvpn/ipp.txt')
threads = Array.new
results = Array.new

connected = false
disconnected = false
as_xml = false

ARGV.each do |arg|
  case arg
  when "--xml"
    as_xml = true
  when "--connected"
    connected = true
  when "--disconnected"
    disconnected = true
  end
end

if !connected and !disconnected
  print "Usage: ovpn-stat.rb [--connected] [--disconnected] [--xml]\n"
  print "    --(dis)connected displays users who fit that connection status.\n"
  print "    --xml returns the result as xml.\n"
  exit
end

ipfile.each do |line|
  threads << Thread.new do
    splitted = line.split(',')
    result = `ping -q -c 1 #{splitted[1]}`
    if($?.exitstatus == 0)
      cstr = "Connected"
    else
      cstr = "Disconnected"
    end
    r = Hash.new
    r["user"] = splitted[0]
    r["ip"] = splitted[1].sub(/\n/, '')
    r["status"] = cstr
    if (cstr == "Connected" and connected) or (cstr == "Disconnected" and discon                                                                                                                                   nected)
      results << r
    end
  end
end

threads.each do |i|
  i.join
end

if as_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.vpnclients {
      results.each do |ci|
      xml.client {
        xml.user ci["user"]
        xml.ip ci["ip"]
        xml.status ci["status"]
      }
      end
    }
  end
  print builder.to_xml
  exit
end

results.each do |i|
  print "#{i["user"]}@#{i["ip"]} - #{i["status"]}\n"
end

It's not quite what you need since it reads the OpenVPN ipp.txt file (all users and their ips since ips are persistent between sessions and it overrides the DHCP server.) With a few modifications this could be something you could use.
 
I usually don't like doing all the work for someone.. but I thought this project would be fun! Plus, I wanted to show the power of a batch file! hehehe There's a lot of easier languages out there and we are all quick to say, "Do it in vb or c# or so on". ...But Some times it's good to write a batch file just because.. hehehe

Code:
@ECHO off

::============================================
:: \\\\\\\\\\\\\\   Variables   //////////////
::============================================
SET drive=c:\nettest
SET OutputFile=result.txt
SET tt=%time:~0,2%_%time:~3,2%_result.txt
SET folder=%date:/=-% %tt%
SET site1=www.google.com
SET site2=www.techpowerup.com


::============================================
:: \\\\\\\\\\\\\\   Message   ////////////////
::============================================

COLOR 9F
ECHO. Welcome to Mindweaver's
ECHO. Cut over test
ECHO. 
ECHO. Ping all sites
PAUSE

GOTO Google

::==================================================
:: \\\\\\\\\\\\\\   Pinging Google  ////////////////
::==================================================
:Google
PING -n 2 %site1% | find "Reply from"

::Check to see if ping was successful or unsuccesful
::successful = :found1
::unsuccessful = :nfound1
IF NOT ERRORLEVEL 1 GOTO found1
IF ERRORLEVEL 1 GOTO nfound1


::Check to see if folder exists
:found1
ECHO.
ECHO Google found!
ECHO.
IF EXIST "%drive%\%folder%" GOTO :write1
IF NOT EXIST "%drive%\%folder%\" GOTO :create1

		::Folder found - writes output
		:write1
		ECHO ping to google successful on Date (%date%), Time (%time%). >>"%drive%\%folder%\%tt%" 
		GOTO techpowerup

		::Folder not found - creates folder and writes output
		:create1
		MD "%drive%\%folder%\"
			
		ECHO ping to google successful on Date (%date%), Time (%time%). >>"%drive%\%folder%\%tt%" 
		GOTO techpowerup

:nfound1
ECHO.
ECHO ###Google NOT FOUND!
ECHO.
IF EXIST "%drive%\%folder%" GOTO :write2
IF NOT EXIST "%drive%\%folder%\" GOTO :create2

		:write2
		ECHO ping to google unsuccessful on Date (%date%), Time (%time%). >>"%drive%\%folder%\%tt%" 
		GOTO techpowerup

		:create2
		MD "%drive%\%folder%\"

		ECHO ping to google unsuccessful on Date (%date%), Time (%time%). >>"%drive%\%folder%\%tt%" 
		GOTO techpowerup

::========================================================
:: \\\\\\\\\\\\\\   Pinging Techpowerup!  ////////////////
::========================================================
:techpowerup
PING -n 2 %site2% | find "Reply from"
IF NOT ERRORLEVEL 1 GOTO found2
IF ERRORLEVEL 1 GOTO nfound2
:found2
ECHO.
ECHO Techpowerup! found!
ECHO ping to Techpowerup! successful on Date (%date%), Time (%time%). >>"%drive%\%folder%\%tt%" 
GOTO Finished

:nfound2
ECHO.
ECHO ###Techpowerup! NOT FOUND!
ECHO ping to Techpowerup! unsuccessful on Date (%date%), Time (%time%). >>"%drive%\%folder%\%tt%" 
GOTO Finished


::==========================================================
:: \\\\\\\\\\\\\\   Cut over test finished  ////////////////
::==========================================================
:Finished
ECHO.
ECHO Cut over test Completed!
ECHO.

@PAUSE

::========================================================================
:: \\\\\\\\\\\\\\   Created by Mindweaver @ Techpowerup!  ////////////////
::========================================================================

EDIT: Added file to download. :toast:
 

Attachments

Last edited:
Man deserves credit
 
I really appreciate it, your 50x better than mine.
I never mean't for someone to write the entire code just point me in the right direction.

May I ask for me to add more sites do I just add more set like below?

SET site3=www.hotmail.com
SET site4=www.awesomer.com

edit: sorry just read the code carefully, need to lots of copy paste. Thanks all!
 
Last edited:
I really appreciate it, your 50x better than mine.
I never mean't for someone to write the entire code just point me in the right direction.

May I ask for me to add more sites do I just add more set like below?

SET site3=www.hotmail.com
SET site4=www.awesomer.com

edit: sorry just read the code carefully, need to lots of copy paste. Thanks all!

No problem Yin. Like I said it sparked my interest.. hehehe Plus, I started out pointing you in the right direction with GOTO. ;) But about adding more sites. Just copy
Code:
::========================================================
:: \\\\\\\\\\\\\\   Pinging Techpowerup!  ////////////////
::========================================================
:techpowerup
PING -n 2 %site2% | find "Reply from"
IF NOT ERRORLEVEL 1 GOTO found2
IF ERRORLEVEL 1 GOTO nfound2
:found2
ECHO.
ECHO Techpowerup! found!
ECHO ping to Techpowerup! successful on Date (%date%), Time (%time%). >>"%drive%\%folder%\%tt%" 
GOTO Finished

:nfound2
ECHO.
ECHO ###Techpowerup! NOT FOUND!
ECHO ping to Techpowerup! unsuccessful on Date (%date%), Time (%time%). >>"%drive%\%folder%\%tt%" 
GOTO Finished
down and then paste it below. Then change where it says, "Techpowerup!" and "site2". You will need to create a new "site3" Variable and your done. :toast:
 
  • Like
Reactions: Yin
Ok... Noob question

How do I make Mindweaver's script create a folder where the bat file is.

eg. we have the bat file on a usb g:\nettest.bat. So the logs save on g:\ instead of c:\.

I tried changing SET drive to = \

but that just breaks the entire script.
 
Sorry, I must of not made that clear.

I meant that the value would be a variable depending on where you run the bat file from.

the usb or the file may not be on g: it could be e: or f:.

I don't know if that made any sense.....
 
Sorry, I must of not made that clear.

I meant that the value would be a variable depending on where you run the bat file from.

the usb or the file may not be on g: it could be e: or f:.

I don't know if that made any sense.....

Use "%~dp0" to find out where the file lives. Change "SET drive=c:\nettest" to "SET drive=%~dp0". You will also need to change "%drive%\" to "%drive%" because "%~dp0" will put the "\" for you. After changing "SET drive=c:\nettest" to "SET drive=%~dp0", I would do a search and replace all "%drive%\" with "%drive%". :toast:
 
thanks a bunch for this , made my job a whole lot easier, got a network of keyboardless computers and onscreen keybord sucks after computer #10.
 
Back
Top