1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Batch file to ping network

Discussion in 'Programming & Webmastering' started by Yin, Jun 26, 2012.

  1. Yin

    Yin

    Joined:
    Feb 19, 2006
    Messages:
    542 (0.17/day)
    Thanks Received:
    32
    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!
     
  2. OnePostWonder

    Joined:
    Dec 27, 2008
    Messages:
    582 (0.27/day)
    Thanks Received:
    107
    I don't know much about programming, but what about an "Else" statement?

    This guy knows more: Batch files branching
     
  3. Yin

    Yin

    Joined:
    Feb 19, 2006
    Messages:
    542 (0.17/day)
    Thanks Received:
    32
    haha we are on the same boat, I'm clueless when it comes to programming.

    I'll give it a go =)
     
  4. Steevo

    Steevo

    Joined:
    Nov 4, 2005
    Messages:
    8,380 (2.55/day)
    Thanks Received:
    1,230
    OneMoar and Yin say thanks.
    10 Million points folded for TPU
  5. Yin

    Yin

    Joined:
    Feb 19, 2006
    Messages:
    542 (0.17/day)
    Thanks Received:
    32
    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?
     
  6. Steevo

    Steevo

    Joined:
    Nov 4, 2005
    Messages:
    8,380 (2.55/day)
    Thanks Received:
    1,230
    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
     
    Yin and Mindweaver say thanks.
    10 Million points folded for TPU
  7. Mindweaver

    Mindweaver Moderato®™ Staff Member

    Joined:
    Apr 16, 2009
    Messages:
    5,268 (2.60/day)
    Thanks Received:
    2,748
    Location:
    Statesville, NC
    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:
     
    Crunching for Team TPU
  8. Yin

    Yin

    Joined:
    Feb 19, 2006
    Messages:
    542 (0.17/day)
    Thanks Received:
    32
    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: Jun 26, 2012
  9. Steevo

    Steevo

    Joined:
    Nov 4, 2005
    Messages:
    8,380 (2.55/day)
    Thanks Received:
    1,230
    Just reuse the same name and it will overwrite the files each time the batch is run.
     
    10 Million points folded for TPU
  10. CaptainFailcon New Member

    Joined:
    Apr 27, 2012
    Messages:
    194 (0.21/day)
    Thanks Received:
    41
    you would be better off writing something in c++ or even VB
    batch file != programming
    batch files are extremely limited
     
  11. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,497 (6.45/day)
    Thanks Received:
    2,197
    Location:
    Concord, NH
    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.
     
  12. Mindweaver

    Mindweaver Moderato®™ Staff Member

    Joined:
    Apr 16, 2009
    Messages:
    5,268 (2.60/day)
    Thanks Received:
    2,748
    Location:
    Statesville, NC
    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:
     

    Attached Files:

    Last edited: Jun 26, 2012
    Yin and Completely Bonkers say thanks.
    Crunching for Team TPU
  13. Completely Bonkers New Member

    Joined:
    Feb 6, 2007
    Messages:
    2,580 (0.91/day)
    Thanks Received:
    516
    Man deserves credit
     
    Mindweaver says thanks.
  14. Mindweaver

    Mindweaver Moderato®™ Staff Member

    Joined:
    Apr 16, 2009
    Messages:
    5,268 (2.60/day)
    Thanks Received:
    2,748
    Location:
    Statesville, NC
    Thanks man! :toast:
     
    Crunching for Team TPU
  15. Yin

    Yin

    Joined:
    Feb 19, 2006
    Messages:
    542 (0.17/day)
    Thanks Received:
    32
    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: Jun 27, 2012
  16. Mindweaver

    Mindweaver Moderato®™ Staff Member

    Joined:
    Apr 16, 2009
    Messages:
    5,268 (2.60/day)
    Thanks Received:
    2,748
    Location:
    Statesville, NC
    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:
     
    Yin says thanks.
    Crunching for Team TPU
  17. Yin

    Yin

    Joined:
    Feb 19, 2006
    Messages:
    542 (0.17/day)
    Thanks Received:
    32
    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.
     
  18. CaptainFailcon New Member

    Joined:
    Apr 27, 2012
    Messages:
    194 (0.21/day)
    Thanks Received:
    41
    SET drive=g:\nettest
    and make sure you replicate the folder
     
  19. Yin

    Yin

    Joined:
    Feb 19, 2006
    Messages:
    542 (0.17/day)
    Thanks Received:
    32
    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.....
     
  20. Mindweaver

    Mindweaver Moderato®™ Staff Member

    Joined:
    Apr 16, 2009
    Messages:
    5,268 (2.60/day)
    Thanks Received:
    2,748
    Location:
    Statesville, NC
    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:
     
    ondove and Yin say thanks.
    Crunching for Team TPU
  21. ondove New Member

    Joined:
    Sep 10, 2014
    Messages:
    1 (0.02/day)
    Thanks Received:
    0
    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.
     

Currently Active Users Viewing This Thread: 4 (0 members and 4 guests)

Share This Page