Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   Dev Hardware ForumsSOFTWAREProgramming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Hardware Forums Sponsor:
  Trader Rating: 0 · #1  
Old March 28th, 2008, 01:32 PM
speeDemon7676 speeDemon7676 is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 1 speeDemon7676 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 52 sec
Reputation Power: 0
Red face Batch File Help?!?

Hey can anyone spot what I am doing wrong here? I want to have the user enter the starting and ending last three digits of an IP address in a certain subnet, then the Batch will go out and ping everyhting between those two numbers. Any help?

@ECHO OFF
:main
IF EXIST C:\PING_RESULTS.txt DEL C:\PING_RESULTS.txt
ECHO Ping Test Ran on %date% at %time% >> C:\PING_RESULTS.txt
ECHO.
goto getips

:GETIPS
ECHO Enter Range of IP's to Ping in the 192.168.1 subnet
ECHO.
SET /P first="Please enter the first number in the range of IP addresses: "
SET /a A=%first%
SET /P second="Please enter the last number in your range of IP addresses to ping: "
SET /a B=%second%
Echo Results for ping test between 192.168.1.%A% and 192.168.1.%B% are as follows: >> C:\PING_RESULTS.txt
goto ping

ing
ECHO.
ping -a 192.168.1.%A% >> C:\PING_RESULTS.txt
IF %A%==%B% GOTO FINISH ELSE %A%=%A%+1
ECHO %A%
GOTO PING


:Finish
set /p yes=Would you like to ping any more addresses (y/n)?
if %yes%=y goto GETIPS ELSE goto end


:end
START Notepad.exe C:\PING_RESULTS.txt

Reply With Quote
  Trader Rating: 0 · #2  
Old March 29th, 2008, 07:50 PM
omrsafetyo's Avatar
omrsafetyo omrsafetyo is offline
Contributing User
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Maine, USA
Posts: 297 omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level)omrsafetyo User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Days 21 h 32 m 32 sec
Reputation Power: 221
Send a message via AIM to omrsafetyo
set /a is used for arithmetic operations.
set /p is used when you want to wait for user input.

The easiest way to fix this would be in the GETIPS section:
Code:
You have:

 ECHO Enter Range of IP's to Ping in the 192.168.1 subnet
 ECHO.
 SET /P first="Please enter the first number in the range of IP addresses: "
 SET /a A=%first%
 SET /P second="Please enter the last number in your range of IP addresses to ping: "
 SET /a B=%second%
 Echo Results for ping test between 192.168.1.%A% and 192.168.1.%B% are as follows: >> C:\PING_RESULTS.txt
 goto ping


Code:
Here is a fix:

 ECHO Enter Range of IP's to Ping in the 192.168.1 subnet
 ECHO.
 SET /P A="Please enter the first number in the range of IP addresses: "
 SET /P B="Please enter the last number in your range of IP addresses to ping: "
Echo Results for ping test between 192.168.1.%A% and 192.168.1.%B% are as follows: >> C:\PING_RESULTS.txt
 goto ping

That gets rid of some extra, unnecessary work that may have been causing issues.
The next portion is in your increment:

Code:
 ping -a 192.168.1.%A% >> C:\PING_RESULTS.txt
 IF %A%==%B% GOTO FINISH
SET /a A=%A%+1
 ECHO %A%
 GOTO PING


There is no else in a batch if statement. If it is true, run the code, otherwise keep going in the code. So in this example, if the A has incremented as far as the B, then quit the script. Otherwise, continue - in which case we increment.

This is where set /a comes in. You can't just arbitrarily re-assign variables like in 4GLs, you have to assign them with SET. In this case, we are adding one to the current value, and thus set /a (arithmetic).

Lastly, you have too many redundant gotos. You have a goto where the next line in the code is the section you are going to. This is redundant, because without the goto call, it would go there anyway. The only place you should have the GOTO command is when you are testing to see if the program is finished, and then going to the proper spot. Our final code:

Code:
 @ECHO OFF
 :main
 IF EXIST C:\PING_RESULTS.txt DEL C:\PING_RESULTS.txt
 ECHO Ping Test Ran on %date% at %time% >> C:\PING_RESULTS.txt
 ECHO.

:GETIPS
 ECHO Enter Range of IP's to Ping in the 192.168.1 subnet
 ECHO.
 SET /P A="Please enter the first number in the range of IP addresses: "
 SET /P B="Please enter the last number in your range of IP addresses to ping: "
Echo Results for ping test between 192.168.1.%A% and 192.168.1.%B% are as follows: >> C:\PING_RESULTS.txt

:PING
 ping -a 192.168.1.%A% >> C:\PING_RESULTS.txt
 IF %A%==%B% GOTO FINISH
 SET /a A=%A%+1
 ECHO %A%
 GOTO PING

:FINISH
 set /p yes=Would you like to ping any more addresses (y/n)?
 if %yes%==y goto GETIPS

START C:\PING_RESULTS.TXT


I have not tested this, as I am on Linux, and as such have a real scripting language at my disposal.

Reply With Quote
  Trader Rating: 0 · #3  
Old July 7th, 2008, 08:28 AM
FamousMortimer FamousMortimer is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 1 FamousMortimer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 52 sec
Reputation Power: 0
Alternate Method

I see what you are trying to do, and I think my batch file does basically the same thing. Try it out and edit it to meet your needs.

Basically all this does is ping the specified range of IP addresses and returns only replies in a results.txt file.

Since its a batch file, it takes long as hell to finish but it gets the job done.

I would like to also have it create several 'threads', i.e. separate batch files that all work independently to ping the specified range, but not quite sure how do do it, as you can't append a line such as
Code:
for /L %%X in (%S%,1,%E%) do echo %A%.%1%%X >>IPs >>thread3.bat
to another batch file because of the multiple >>'s.

If anyone has any ideas on how to do it, let me know.

Code:
@echo off
del IPs
del temp
cls

::Append Header to Results File

if exist Results.txt (
echo. >>Results.txt
echo --------------------------------- >>Results.txt
echo. >>Results.txt
)

if not exist Results.txt (
echo +-------------------------------+ > Results.txt
echo ¦    IP Pinger by BeRsErKeR     ¦ >>Results.txt
echo +-------------------------------+ >>Results.txt
echo. >>Results.txt
echo Test Started on %date% at %time%. >>Results.txt
echo. >>Results.txt
echo. >>Results.txt
)

::Define Variables

set S=1
set E=254

::Get Range to Ping

set /p A=Enter the first 3 octets of the IP address (e.g. 192.168.1): 
set /p S=Enter the starting range (1 to 254) (Enter for 1):
set /p E=Enter the ending range (1 to 254) (Enter for 254):
cls

::Create IP List File

for /L %%X in (%S%,1,%E%) do echo %A%.%1%%X >>IPs

::Ping IP's in Generated File

for /f "tokens=1" %%p, in (IPs) do (
	echo Pinging %%p
	ping -n 1 %%p > temp
	findstr /C:"Reply from" temp >> Results.txt
)

::Cleanup Temp Files

del IPs
del temp

::Show Results

Start Results.txt

exit


It seems to me that you want to log all results regardless of successful or not, so in that case you can change line
Code:
findstr /C:"Reply from" temp >> Results.txt 
to
Code:
findstr "timed Reply" temp >> Results.txt
and to see what IP it times out on add
Code:
echo %%p >> Results.txt 
just above it.

Hope this is help to some of you

Reply With Quote
  Trader Rating: 2 · #4  
Old July 26th, 2008, 07:25 PM
Nilpo's Avatar
Nilpo Nilpo is offline
Dev Hardware God (5000 - 5499 posts)
 
Join Date: May 2004
Location: Salem, OH
Posts: 5,344 Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)Nilpo User rank is General 68th Grade (Above 100000 Reputation Level)  Folding Points: 189852 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189852 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189852 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189852 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189852 Folding Title: Super Ultimate Folder - Level 1Folding Points: 189852 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 Week 2 Days 21 h 59 m 7 sec
Reputation Power: 5514
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
MySpace Orkut
In case you are interested. Here's what it looks like in WSH:
vb Code:
Original - vb Code
  1. Set objFso = CreateObject("Scripting.FileSystemObject")
  2. Set objLog = objFso.CreateTextFile("C:\PING_RESULTS.txt", True)
  3. Set objWMIService = GetObject("winmgmts://./root/cimv2")
  4.  
  5. Call Main
  6.  
  7. Sub Main
  8.     intLow = InputBox("Please enter a starting number.", "Enter Range of IPs", "0")
  9.     intHigh = InputBox("Please enter an ending number.", "Enter Range of IPs", "255")
  10.  
  11.     If intLow = "" Or intHigh = "" Then WScript.Quit
  12.  
  13.     objLog.WriteLine "Ping Test Ran on " & Date() & " at " & Time()
  14.     objLog.WriteLine "Results for ping test between 192.168.1." & intLow & " and 192.168.1."& intHigh &":"
  15.  
  16.     For i = intLow To intHigh
  17.         strIP = "192.168.1." & i
  18.         objLog.WriteLine FormatIP(strIP) & PingResult(strIP)
  19.     Next
  20.  
  21.     WScript.Run "notepad.exe C:\PING_RESULTS.txt"
  22.  
  23.     result = MsgBox("Would you like to ping more addresses?", vbYesNo + vbQuestion, "Ping Test")
  24.     If result = vbYes Then Main
  25. End Sub
  26.  
  27. Function PingResult(strTarget)
  28.     Set colPings = objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = '" & strTarget & "'")
  29.     For Each objPing In colPings
  30.         If objPing.StatusCode = 0 Then
  31.             PingResult = "Success"
  32.         Else
  33.             PingResult = "Failure"
  34.         End If
  35.     Next
  36. End Function
  37.  
  38. Function FormatIP(strIP)
  39.     intPadding = 15 - Len(strIP)
  40.     FormatIP = strIP & Space(intPadding)
  41. End Function
It results in a log like the following:
Code:
Ping Test Ran on 7/26/2008 at 8:26:48 PM
Results for ping test between 192.168.1.0 and 192.168.1.10:
192.168.1.0    Failure
192.168.1.1    Success
192.168.1.2    Failure
192.168.1.3    Failure
192.168.1.4    Success
192.168.1.5    Failure
192.168.1.6    Failure
192.168.1.7    Failure
192.168.1.8    Failure
192.168.1.9    Success
192.168.1.10   Failure
__________________
Click the image if at any point you don't like my decision.

10011100011000101111100011100000011000000100000011 10010011011110110001101101011011110100011000001110 0100111101000100001

Visit Nilpo.com and Ask the Windows Guru!

Open me for some very useful links!    

Last edited by Nilpo : July 26th, 2008 at 07:28 PM.

Reply With Quote
Reply

Viewing: Dev Hardware ForumsSOFTWAREProgramming > Batch File Help?!?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway