Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Samir
- Posts: 384
- Joined: 16 Jul 2013 12:00
- Location: HSV
-
Contact:
#1
Post
by Samir » 18 Oct 2015 08:46
I had to generate some random private addresses for testing our network and was surprised that in all my Internet searches, nothing existed to make this happen.
I ended up using some free online random number generators to generate each octet and got the job done, but I'm sure there is a nice batch that can be created to do this.
Anyone want to try to make such a batch?
I think it should have command line parameters for choosing what type of private address space (192.168.x.x or 172.16-32.x.x or 10.x.x.x) and the number of addresses. So use would be something like this:
for a result of:
Code: Select all
192.168.x.x
192.168.x.x
192.168.x.x
192.168.x.x
192.168.x.x
I found this batch that someone had made for a random ip address and I'm guessing it could be adapted:
http://www.instructables.com/id/Random-IP-Generator/I'd work on this myself as it seems like a fun exercise, but I have some deadlines next week that I have to work on right now.
-
npocmaka_
- Posts: 516
- Joined: 24 Jun 2013 17:10
- Location: Bulgaria
-
Contact:
#2
Post
by npocmaka_ » 18 Oct 2015 09:41
Code: Select all
:randip
@echo off
setlocal
set "first="
set "second="
set "third="
set "fourth="
if "%~1" neq "" (
set /a "first=%~1"
)
if "%~2" neq "" (
set /a "second=%~2"
)
if "%~3" neq "" (
set /a "third=%~3"
)
set /a fourth=%random%%%256
if not defined first set /a first=%random%%%256
if not defined second set /a second=%random%%%256
if not defined third set /a third=%random%%%256
echo %first%.%second%.%third%.%fourth%
endlocal
?
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#3
Post
by penpen » 18 Oct 2015 10:15
"rndIp.bat":
Code: Select all
@echo off
setlocal enableExtensions enableDelayedExpansion
set "N=%~1"
set "m=%~2.x.x.x.x"
for /F "tokens=1-4 delims=." %%a in ("%m:x=0-255%") do (
set "m[3]=%%~a"
set "m[2]=%%~b"
set "m[1]=%%~c"
set "m[0]=%%~d"
)
for /L %%N in (1, 1, %N%) do (
set "ip="
set /A "r=(!random!<<16)|!random!", "r[3]=(r>>24)&0xFF", "r[2]=(r>>16)&0xFF", "r[1]=(r>>8)&0xFF", "r[0]=r&0xFF"
for /L %%i in (3, -1, 0) do for /F "tokens=1* delims=-" %%a in ("!m[%%~i]!") do (
if "%%~a" == "%%~b" ( set /A "v=%%~a"
) else if "%%~b" == "" ( set /A "v=%%~a"
) else ( set /A "v=(!r[%%~i]!%%(%%~b-%%~a+1))+%%~a"
)
set "ip=!ip!.!v!"
)
echo(!ip:~1!
)
endlocal
:: Usage: rndIp.bat numIps [mask]
Code: Select all
Z:\>rndIp.bat 2
29.35.119.55
69.94.80.113
Z:\>rndIp.bat 4 192.168.2-8.x
192.168.7.77
192.168.8.23
192.168.8.45
192.168.8.42
penpen
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#4
Post
by Aacini » 18 Oct 2015 18:02
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem RandomIPs.bat: Generate a series of IP's with range/random values
rem Antonio Perez Ayala
if "%~1" neq "" if "%~1" neq "/?" goto begin
echo/
echo Usage: %~NX0 [num] mask
echo/
echo - The "mask" is comprised of 4 parts separated by dots: 192.168.8.0
echo - Each part may be comprised of two numbers separated by dash,
echo indicating a range of numbers: 192.168.8.0-16
echo - Each part may be comprised of an "x" letter (random number): 192.168.8.x
echo - After the "x" a range of random numbers may be given: 192.168.8.x16-32
echo - If an "x" is given generate one IP, or the number given by "num" parameter;
echo this is done for each one of the numbers in the range given in other parts.
echo A couple examples that generate 8 IP's: 8 192.168.8.x 2 192.168.8-11.x
echo - The "mask" may also be comprised of 6 parts: 192.168.8.0.10.20
goto :EOF
:begin
set "num=1"
set "mask=%1"
if "%mask:.=%" equ "%mask%" (
set "num=%1"
set "mask=%2"
)
set expr=set /A "0
set "letter=abcdef"
set ipv=0
set "addr="
(
for %%a in (%mask:.=,%) do (
set "x=!letter:~0,1!"
set /A ipv+=1
set "part=%%a"
if /I "!part:~0,1!" neq "x" (
for /F "tokens=1,2 delims=-" %%y in ("%%a") do (
set "x1=%%y"
if "%%z" equ "" (set "x2=%%y") else set "x2=%%z"
)
set "addr=!addr!.%%%%!x!"
) else (
set /A "x1=1,x2=num,num=1"
if /I "!part:~-1!" equ "x" (
set "expr=!expr!,!x!=^!random^!%%%%255"
) else (
for /F "tokens=1,2 delims=-" %%y in ("!part:~1!") do (
set "expr=!expr!,!x!=^!random^!%%%%(%%z-%%y+1)+%%y"
)
)
set "addr=!addr!.^!!x!^!"
)
echo for /L %%%%!x! in (!x1!,1,!x2!^) do (
set "letter=!letter:~1!"
)
echo !expr!"
echo echo !addr:~1!
for /L %%i in (1,1,!ipv!) do set /P "=)" < NUL
echo/
) > ShowIPs.bat
ShowIPs.bat
Antonio