-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipRange.bat
More file actions
266 lines (113 loc) · 3.35 KB
/
ipRange.bat
File metadata and controls
266 lines (113 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
@echo off
setlocal EnableExtensions EnableDelayedExpansion
:: ============================================================
:: ipRange.bat
::
:: Simple IP range ping sweep for Windows 8+ (CMD only)
:: Default: prints ONLY responsive IP addresses
:: ============================================================
set QUIET=0
set COUNT_ONLY=0
set "OUTFILE="
set ADDRESS=
:: ---- Defaults ----
set START=1
set END=255
set TIMEOUT=500
:: ---- Parse args (options allowed anywhere) ----
:PARSE
if "%~1"=="" goto :VALIDATE
if /I "%~1"=="--help" goto :HELP
if /I "%~1"=="-h" goto :HELP
if "%~1"=="/?" goto :HELP
if /I "%~1"=="--quiet" (set QUIET=1 & shift & goto :PARSE)
if /I "%~1"=="-q" (set QUIET=1 & shift & goto :PARSE)
if /I "%~1"=="--count" (set COUNT_ONLY=1 & shift & goto :PARSE)
if /I "%~1"=="--output" goto :PARSE_OUTPUT
if /I "%~1"=="-o" goto :PARSE_OUTPUT
:: ---- Positional arguments ----
if not defined ADDRESS (
set "ADDRESS=%~1" & shift & goto :PARSE
)
if "%START%"=="1" (
set START=%~1 & shift & goto :PARSE
)
if "%END%"=="255" (
set END=%~1 & shift & goto :PARSE
)
set TIMEOUT=%~1
shift
goto :PARSE
:PARSE_OUTPUT
shift
if "%~1"=="" exit /b 2
set "OUTFILE=%~1"
shift
goto :PARSE
:VALIDATE
if not defined ADDRESS goto :HELP
:: ---- Numeric validation ----
set /a _S=%START% >nul 2>nul || exit /b 2
set /a _E=%END% >nul 2>nul || exit /b 2
set /a _T=%TIMEOUT% >nul 2>nul || exit /b 2
set START=%_S%
set END=%_E%
set TIMEOUT=%_T%
:: ---- Bounds ----
if %START% LSS 1 exit /b 2
if %START% GTR 255 exit /b 2
if %END% LSS 1 exit /b 2
if %END% GTR 255 exit /b 2
if %START% GTR %END% exit /b 2
if %TIMEOUT% LSS 1 exit /b 2
if %TIMEOUT% GTR 60000 set TIMEOUT=60000
:: ---- Validate ADDRESS (exactly X.Y.Z) ----
set O1=
set O2=
set O3=
set O4=
for /f "tokens=1-4 delims=." %%A in ("%ADDRESS%") do (
set O1=%%A
set O2=%%B
set O3=%%C
set O4=%%D
)
if "%O1%"=="" exit /b 2
if "%O2%"=="" exit /b 2
if "%O3%"=="" exit /b 2
if not "%O4%"=="" exit /b 2
for %%O in (%O1% %O2% %O3%) do (
set /a N=%%O 2>nul || exit /b 2
if !N! LSS 0 exit /b 2
if !N! GTR 255 exit /b 2
)
:: ---- Prepare output file ----
if defined OUTFILE ( > "%OUTFILE%" (type nul) || exit /b 2 )
set FOUND=0
set COUNT=0
:: ---- Ping sweep ----
for /L %%I in (%START%,1,%END%) do (
ping -n 1 -w %TIMEOUT% %ADDRESS%.%%I >nul 2>&1
if not errorlevel 1 (
set FOUND=1
set /a COUNT+=1
if defined OUTFILE echo %ADDRESS%.%%I>>"%OUTFILE%"
if %QUIET%==0 if %COUNT_ONLY%==0 echo %ADDRESS%.%%I
)
)
if %QUIET%==0 if %COUNT_ONLY%==1 echo %COUNT%
endlocal & ( if "%FOUND%"=="1" (exit /b 0) else (exit /b 1) )
:HELP
echo.
echo ipRange.bat - IP range ping sweep
echo.
echo USAGE:
echo ipRange.bat [options] ^<X.Y.Z^> [start] [end] [timeout_ms]
echo.
echo OPTIONS:
echo --help ^| -h ^| /? Show this help
echo --quiet ^| -q Suppress console output
echo --count Print only total responsive hosts
echo --output ^<file^> ^| -o Write responsive IPs to file
echo.
exit /b 0