![]() |
|
|
#1 |
![]() |
Batch file to ping network
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 Thanks! |
|
|
|
|
|
#2 |
![]() |
I don't know much about programming, but what about an "Else" statement?
This guy knows more: Batch files branching |
|
|
|
|
|
#3 |
![]() |
haha we are on the same boat, I'm clueless when it comes to programming.
I'll give it a go =) |
|
|
|
|
|
#4 |
|
Eligible for custom title
|
__________________
“it would have been perfect....its got trains and the line"tech your kids not to do what iv done"(or similar) because i had obviously done something to warrent 2 e-thugs to come 4000miles out of their way and kill me.” -Solaris17 “yeah i failed. i noticed the "coming soon" part after i posted.” -Mussels
“people are just stupid.” -W1zzard
Yes I am evil, yes you can have some.
|
|
|
|
| The Following User Says Thank You to Steevo For This Useful Post: |
|
|
#5 |
![]() |
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 |
|
Eligible for custom title
|
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
__________________
“it would have been perfect....its got trains and the line"tech your kids not to do what iv done"(or similar) because i had obviously done something to warrent 2 e-thugs to come 4000miles out of their way and kill me.” -Solaris17 “yeah i failed. i noticed the "coming soon" part after i posted.” -Mussels
“people are just stupid.” -W1zzard
Yes I am evil, yes you can have some.
|
|
|
|
| The Following 2 Users Say Thank You to Steevo For This Useful Post: |
|
|
#7 |
|
Moderato®™
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,651 (2.44/day)
Thanks: 4,320
Thanked 2,319 Times in 1,150 Posts
|
You need to use GOTO
![]() EDIT: I would write an example.. but I'm off to bed. If you are still having problems tomorrow. I'll chime in. Steevo is on the right track!
__________________
“As long as I feel the warmth from the sun and breathe precious air…. I must ask questions to feed the Mind!” Battletag: Mindweaver#1523 |
|
|
|
|
|
#8 |
![]() |
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 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 by Yin; Jun 26, 2012 at 04:43 AM. |
|
|
|
|
|
#9 |
|
Eligible for custom title
|
Just reuse the same name and it will overwrite the files each time the batch is run.
__________________
“it would have been perfect....its got trains and the line"tech your kids not to do what iv done"(or similar) because i had obviously done something to warrent 2 e-thugs to come 4000miles out of their way and kill me.” -Solaris17 “yeah i failed. i noticed the "coming soon" part after i posted.” -Mussels
“people are just stupid.” -W1zzard
Yes I am evil, yes you can have some.
|
|
|
|
|
|
#10 |
|
Banned
Join Date: Apr 2012
Posts: 193 (0.50/day)
Thanks: 12
Thanked 41 Times in 38 Posts
|
you would be better off writing something in c++ or even VB
batch file != programming batch files are extremely limited |
|
|
|
|
|
#11 |
![]() Join Date: Jan 2012
Location: Dover, New Hampshire, USA
Posts: 4,246 (8.85/day)
Thanks: 1,269
Thanked 1,324 Times in 982 Posts
|
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
__________________
MyHeat |
|
|
|
|
|
#12 |
|
Moderato®™
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,651 (2.44/day)
Thanks: 4,320
Thanked 2,319 Times in 1,150 Posts
|
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! //////////////// ::========================================================================
__________________
“As long as I feel the warmth from the sun and breathe precious air…. I must ask questions to feed the Mind!” Battletag: Mindweaver#1523 Last edited by Mindweaver; Jun 26, 2012 at 09:35 PM. |
|
|
|
| The Following 2 Users Say Thank You to Mindweaver For This Useful Post: |
|
|
#13 |
![]() |
Man deserves credit
__________________
... some things in life just drive you bonkers. Especially the rubbish you see in forum posts |
|
|
|
| The Following User Says Thank You to Completely Bonkers For This Useful Post: |
|
|
#14 |
|
Moderato®™
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,651 (2.44/day)
Thanks: 4,320
Thanked 2,319 Times in 1,150 Posts
|
__________________
“As long as I feel the warmth from the sun and breathe precious air…. I must ask questions to feed the Mind!” Battletag: Mindweaver#1523 |
|
|
|
|
|
#15 |
![]() |
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 by Yin; Jun 27, 2012 at 05:24 AM. |
|
|
|
|
|
#16 | |
|
Moderato®™
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,651 (2.44/day)
Thanks: 4,320
Thanked 2,319 Times in 1,150 Posts
|
Quote:
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
__________________
“As long as I feel the warmth from the sun and breathe precious air…. I must ask questions to feed the Mind!” Battletag: Mindweaver#1523 |
|
|
|
|
| The Following User Says Thank You to Mindweaver For This Useful Post: |
|
|
#17 |
![]() |
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 |
|
Banned
Join Date: Apr 2012
Posts: 193 (0.50/day)
Thanks: 12
Thanked 41 Times in 38 Posts
|
SET drive=g:\nettest
and make sure you replicate the folder |
|
|
|
|
|
#19 |
![]() |
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 | |
|
Moderato®™
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,651 (2.44/day)
Thanks: 4,320
Thanked 2,319 Times in 1,150 Posts
|
Quote:
__________________
“As long as I feel the warmth from the sun and breathe precious air…. I must ask questions to feed the Mind!” Battletag: Mindweaver#1523 |
|
|
|
|
| The Following User Says Thank You to Mindweaver For This Useful Post: |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Batch file programming | boogerlad | Programming & Webmastering | 4 | Mar 4, 2011 06:13 AM |
| Batch file helpies! | <<Onafets>> | Programming & Webmastering | 5 | Nov 22, 2009 05:05 AM |
| dx 10.1 ping pong demo missing file | Darkmind | General Software | 2 | Apr 21, 2008 12:02 AM |
| Batch File Recognition | PVTCaboose1337 | Programming & Webmastering | 6 | Jan 17, 2008 06:36 AM |
| Mapped network drive batch file? | Echelon | General Software | 0 | Jan 7, 2008 09:54 PM |