Batch Script for generating random ip addresses

Discussion in 'Programming' started by Cleptography, Sep 6, 2010.

  1. Cleptography

    Cleptography New Member

    Joined:
    Sep 2, 2010
    Messages:
    39
    Likes Received:
    7
    Trophy Points:
    0
    This is a two part batch script that generates a specified number of random ip addresses, or can take input from an external script file to pass to the utility. There are much faster ways of doing this of course but hey it works. Save as GenIps.bat and RunIps.bat in the same directory. From the command line type GenIps to retrieve usage info.

    Batch Script for generating random ip addresses.
    --------------------------------------------------------
    (GENIPS.BAT)
    Code:
    @echo off
    :: Script 1
    
    REM The first numerical digit in a scope represents the value to not exceed
    REM The second numerical digit represents the value to not go below
    
    :SET_LOCAL_ENVIRONMENT
    REM Set the number of Ips to generate
    	set one=%~1
    
    REM Set the first group 1/4 scope ip range
    	set two=%~2
    	set three=%~3
    
    REM Set the second group 2/4 scope ip range
    	set four=%~4
    	set five=%~5
    
    REM Set the third group 3/4 scope ip range
    	set six=%~6
    	set seven=%~7
    
    REM Set the fourth group 4/4 scope ip range
    	set eight=%~8
    	set nine=%~9
    
    REM Check user parameters
    :USER_PARAMETERS
    	if /i [%~1]==[] (goto :USAGE)
    	if /i [%~1]==[-script] (if /i [%~2]==[-?] (goto :USAGE2))
    	if /i [%~1]==[-script] (goto :RUN_SCRIPT)
    
    REM Check if using external script
    	if %~1 leq 1 goto :RUN_IPS
    	if %~1 geq 1 goto :RUN_IPS
    
    REM Pass command line parameters to child application RunIps
    :RUN_IPS
    	set CommandCode=1
    	call RunIps
    	goto :eof
    
    REM Pass script parameters to child application RunIps
    :RUN_SCRIPT
    	set CommandCode=2
    	for /f "skip=1 tokens=*" %%a in (%~2) do (
    	RunIps %%a
    	)
    	goto :eof
    
    REM Command line usage
    :USAGE
    	cls
    	echo USAGE:  
    	echo.
    	echo  GenIps [# x's 2 -^>] [9 Args]
    	echo.
    	echo EXAMPLE:
    	echo.
    	echo  GenIps 25 255 192 150 50 250 120 170 40
    	echo.
    	echo NOTES:
    	echo.
    	echo  The 1st digit represents the number of
    	echo  Ips to produce while the next 8 digits
    	echo  Grouped in sets of 2 represent one of
    	echo  The four places within an ip address
    	echo  The first digit in a group represents
    	echo  The max value while the second digit
    	echo  Represents the low value. Low values
    	echo  Should never be greater than the max
    	echo  Values they preceed.
    	echo  At least 2 ips are needed to Generate
    	echo  A successfull exit code
    	echo.
    	echo  Type: "GenIps -script -?" to retrieve
    	echo  The script usage within GenIps
    	echo.
    	echo DETAILED_EXAMPLES:
    	echo.
    	echo  GenIps 2 192 192 168 168 1 1 100 50
    	echo.
    	echo  The above command would produce output
    	echo  like so . . .
    	echo.
    	echo  192.168.1.78
    	echo  192.168.1.92
    	echo.
    	echo  Taking the name of the program as parameter "%%0"
    	echo  %%1 == "2" being the number of Ips to generate
    	echo  . . . and parameters %%2-%%9 being the average
    	echo  If you use the same values for both greater than
    	echo  And less than that place holder will stay as is
    	echo  Another example . . .
    	echo.
    	echo  GenIps 5 255 192 255 168 200 100 255 143
    	echo.
    	echo  Would produce output like so . . .
    	echo.
    	echo  217.235.113.253
    	echo  213.179.161.214
    	echo  236.231.146.204
    	echo  194.170.102.149
    	echo  226.209.115.145
    	echo.
    	echo BROKEN_DOWN:
    	echo.
    	echo  CommandName NumberOfIps Group01 Group02 Group03 Group04
    	echo  ----------- ----------- ------- ------- ------- -------
    	echo     GenIps        5      255 192 255 168 200 100 255 143
    	echo.
    	echo GROUPS:
    	echo.
    	echo  A group consist of two numerical values single spaced
    	echo  255 192 under "Group01" would be our first group in
    	echo  The above example.
    	echo  The first value is the number that will not be exceeded
    	echo  While the second value represents the number to not go
    	echo  Below.
    	goto :eof
    
    REM Script usage
    :USAGE2
    	cls
    	echo USAGE:
    	echo.
    	echo  GenIps [switch] [FileName]
    	echo.
    	echo EXAMPLE:
    	echo.
    	echo  GenIps -script MyFile.txt
    	echo.
    	echo NOTES:
    	echo.
    	echo  Script Ip files are plain text
    	echo  Files that house all numerical
    	echo  Values derived from the childs
    	echo  Application RunIps.
    	echo  The 1st parameters in a script
    	echo  Should be the number of ips to
    	echo  Generate not the command GenIp
    	echo  The 1st line in the script txt
    	echo  Should be the name of the file
    	echo  Or some sort of header like in
    	echo  C or an Include reference.
    	echo.
    	echo SCRIPT_EXAMPLE:
    	echo.
    	echo -------------------------------
    	echo ^<GenIp_Script_File^>
    	echo.
    	echo  7 192 167 255 11 192 70 50 10
    	echo  5 255 190 255 50 192 30 95 80
    	echo  3 192 167 255 67 192 30 99 20
    	echo  9 255 190 255 92 192 30 95 20
    	echo  4 192 192 255 20 192 30 90 20
    	echo  2 255 190 255 88 192 30 95 20
    	echo -------------------------------
    	echo.
    	echo  Copy n paste everything within
    	echo  The dashed lines and save as a
    	echo  txt file and then from the cmd
    	echo  Line type:
    	echo  GenIps -script [File Name].txt
    	echo  To engage the scripts contents
    	goto :eof
    
    :MISSING_FILES
    	cls
    	echo ERROR:
    	echo.
    	echo  File "RunIps.bat" is missing or was not found in your path
    	echo  Add "RunIps.bat" to your path or put the file in the same 
    	echo  Directory as "GenIps.bat" and restart the application
    	echo  Application now exiting . . .
    	goto :eof

    (RUNIPS.BAT)
    Code:
    @echo Off
    :: Script 2
    
    REM Check Args from parent application GenIps
    	if /i [%CommandCode%]==[] (goto :MISSING_GENIPS)
    	if /i [%CommandCode%]==[1] (goto :RUN_COMMAND)
    	if /i [%CommandCode%]==[2] (goto :RUN_SCRIPT)
    
    REM Begin generating random ip addresses
    :RUN_COMMAND
    	set LoopNumber=1
    	echo.
    	
    :BEGIN
    	set /a LoopNumber=%LoopNumber%+1
    	if "%LoopNumber%" == "%one%" goto :END1
    	
    :A
    	if /i [%two%]==[%three%] (set IPone=%two%) & (goto :B)
    	set IPone=%Random%
    	if %IPone% GTR %two% goto :A
    	if %IPone% LSS %three% goto :A
    	goto :B
    	
    :B
    	if /i [%four%]==[%five%] (set IPtwo=%four%) & (goto :C)
    	set IPtwo=%Random%
    	if %IPtwo% GTR %four% goto :B
    	if %IPtwo% LSS %five% goto :B
    	goto :C
    	
    :C
    	if /i [%six%]==[%seven%] (set IPthr=%six%) & (goto :D)
    	set IPthr=%Random%
    	if %IPthr% GTR %six% goto :C
    	if %IPthr% LSS %seven% goto :C
    	goto :D
    	
    :D
    	if /i [%eight%]==[%nine%] (set IPfor=%eight%) & (goto :E)
    	set IPfor=%Random%
    	if %IPfor% GTR %eight% goto :D
    	if %IPfor% LSS %nine% goto :D
    	goto :E
    	
    :E
    	echo %IPone%.%IPtwo%.%IPthr%.%IPfor%
    	goto :BEGIN
    	
    :END1
    	set LoopNumber=1
    	
    :BEGIN2
    	set /a LoopNumber=%LoopNumber%+1
    	if "%LoopNumber%" == "4" goto :END2
    	
    :A2
    	if /i [%two%]==[%three%] (set IPone=%two%) & (goto :B2)
    	set IPone=%Random%
    	if %IPone% GTR %two% goto :A2
    	if %IPone% LSS %three% goto :A2
    	goto :B2
    	
    :B2
    	if /i [%four%]==[%five%] (set IPtwo=%four%) & (goto :C2)
    	set IPtwo=%Random%
    	if %IPtwo% GTR %four% goto :B2
    	if %IPtwo% LSS %five% goto :B2
    	goto :C2
    	
    :C2
    	if /i [%six%]==[%seven%] (set IPthr=%six%) & (goto :D2)
    	set IPthr=%Random%
    	if %IPthr% GTR %six% goto :C2
    	if %IPthr% LSS %seven% goto :C2
    	goto :D2
    	
    :D2
    	if /i [%eight%]==[%nine%] (set IPfor=%eight%) & (goto :E2)
    	set IPfor=%Random%
    	if %IPfor% GTR %eight% goto :D2
    	if %IPfor% LSS %nine% goto :D2
    	goto :E2
    	
    :E2
    	echo %IPone%.%IPtwo%.%IPthr%.%IPfor%
    	goto :BEGIN2
    	
    :END2
    	set CommandCode=
    	goto :eof
    
    REM Begin generating ip addresses from script command
    :RUN_SCRIPT
    	set LoopNumber=1
    	echo.
    	
    :BEGIN3
    	set /a LoopNumber=%LoopNumber%+1
    	if "%LoopNumber%" == "%~1" goto :END3
    	
    :AA
    	if /i [%~2]==[%~3] (set IPone=%~2) & (goto :BB)
    	set IPone=%Random%
    	if %IPone% GTR %~2 goto :AA
    	if %IPone% LSS %~3 goto :AA
    	goto :BB
    	
    :BB
    	if /i [%~4]==[%~5] (set IPone=%~4) & (goto :CC)
    	set IPtwo=%Random%
    	if %IPtwo% GTR %~4 goto :BB
    	if %IPtwo% LSS %~5 goto :BB
    	goto :CC
    	
    :CC
    	if /i [%~6]==[%~7] (set IPone=%~6) & (goto :DD)
    	set IPthr=%Random%
    	if %IPthr% GTR %~6 goto :CC
    	if %IPthr% LSS %~7 goto :CC
    	goto :DD
    	
    :DD
    	if /i [%~8]==[%~9] (set IPone=%~8) & (goto :EE)
    	set IPfor=%Random%
    	if %IPfor% GTR %~8 goto :DD
    	if %IPfor% LSS %~9 goto :DD
    	goto :EE
    	
    :EE
    	echo %IPone%.%IPtwo%.%IPthr%.%IPfor%
    	goto :BEGIN3
    	
    :END3
    	set LoopNumber=1
    	
    :BEGIN4
    	set /a LoopNumber=%LoopNumber%+1
    	if "%LoopNumber%" == "4" goto :END4
    	
    :AA2
    	if /i [%~2]==[%~3] (set IPone=%~2) & (goto :BB2)
    	set IPone=%Random%
    	if %IPone% GTR %~2 goto :AA2
    	if %IPone% LSS %~3 goto :AA2
    	goto :BB2
    	
    :BB2
    	if /i [%~4]==[%~5] (set IPone=%~4) & (goto :CC2)
    	set IPtwo=%Random%
    	if %IPtwo% GTR %~4 goto :BB2
    	if %IPtwo% LSS %~5 goto :BB2
    	goto :CC2
    	
    :CC2
    	if /i [%~6]==[%~7] (set IPone=%~6) & (goto :DD2)
    	set IPthr=%Random%
    	if %IPthr% GTR %~6 goto :CC2
    	if %IPthr% LSS %~7 goto :CC2
    	goto :DD2
    	
    :DD2
    	if /i [%~8]==[%~9] (set IPone=%~8) & (goto :EE2)
    	set IPfor=%Random%
    	if %IPfor% GTR %~8 goto :DD2
    	if %IPfor% LSS %~9 goto :DD2
    	goto :EE2
    	
    :EE2
    	echo %IPone%.%IPtwo%.%IPthr%.%IPfor%
    	goto :BEGIN4
    	
    :END4
    	set CommandCode=
    	goto :eof
    
    :MISSING_GENIPS
    	cls
    	echo ERROR:
    	echo.
    	echo  To run this script use its parent application
    	echo  To engage the local programs environment vars
    	echo  Type: GenIps without args to display the help
    	goto :eof
    
    :MISSING_FILES
    	cls
    	echo ERROR:
    	echo.
    	echo  File "GenIps.bat" is missing or was not found in your path
    	echo  Add "GenIps.bat" to your path or put the file in the same 
    	echo  Directory as "RunIps.bat" and restart the application from
    	echo  Its parent application "GenIps.bat"
    	echo  Application now exiting . . .
    	goto :eof
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  4. tennsoccerdr

    tennsoccerdr New Member

    Joined:
    Oct 19, 2010
    Messages:
    24
    Likes Received:
    1
    Trophy Points:
    0
    Quick question: What would be the best use for this?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice