简体   繁体   中英

Alternative to grep for isolating specific text in Windows command line

My network changes rather dynamically and unfortunately I am forced to run Windows.

This script works because I have installed GNU tools for Windows and can use grep.

I would like to be able to run this script on any windows machine without having to install anything (grep etc). I used findstr in this at first but could not get it to show only what matches the regex string.

@echo off

set %IPLIST% = nul
Echo.
Echo "Pinging all local Gateways"
    ipconfig | findstr /i "Gateway" | grep -o "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" > %IPLIST%

For /F "Usebackq Delims=" %%# in ("%IPLIST%") 
do (
    Echo+
    Echo [+] Pinging: %%#

Ping -n 1 "%%#" 1>nul && (
    Echo     [OK]) || (
    Echo     [FAILED])
)

Echo.
Echo.

Echo "now testing your internet connection"
Echo .
Echo ...

if %errorlevel% == 0 (
    echo ....you're all good bro.
) else (
    ping -n 3 "8.8.8.8" | findstr /r /c:"[0-9] *ms"
    if %errorlevel% == 1 (
echo ....all is lost
)
)
Echo.

You have a number of problems with the script.

set %IPLIST% = nul

This would yield a syntax error because iplist is likely undefined, and you are attempting to set the variable contents of iplist to "nul"

The resolution of this would be

set  = nul

Further, spaces on either side of the = are included in the variable/value set, so removing the % s would set the value of iplist Space to Space nul

Had iplist indeed been set up to nul then your ipconfig command would have simply dumped its eventual output into the bit-bucket as it would have been sent to nul .

For /F "Usebackq Delims=" %%# in ("%IPLIST%") 

The do ( must be on the same physical line as the "for"

<opinion>Using non-alphas as metavariables is not a good idea as it is not officially supported <\opinion>

if %errorlevel% == 0 (
    echo ....you're all good bro.
) else (
    ping -n 3 "8.8.8.8" | findstr /r /c:"[0-9] *ms"
    if %errorlevel% == 1 (
echo ....all is lost
)
)

Because of the delayed expansion trap (all %var% in a block (parenthesised sequence of statements) are replaced by their value at parse-time ) the innermost %errorlevel% here would be replaced by whatever errorlevel was set to when the outermost if was encountered. To interpret the ping output, you'd need to either invoke delayedexpansion (many, many SO items on this subject) or use the old original if errorlevel n... construct, where the if condition is true if the current errorlevel is n or greater than n .

So - reconstructing using batch features,

@ECHO OFF
SETLOCAL

Echo.
Echo "Pinging all local Gateways"
For /F "tokens=2 Delims=:" %%a in ('ipconfig ^| findstr /i "Gateway"') DO IF "%%a" neq " " (
    Echo+
    Echo [+] Pinging: %%a

Ping -n 1 %%a 1>nul && (
    Echo     [OK]) || (
    Echo     [FAILED])
)

Echo.
Echo.

Echo "now testing your internet connection"
Echo .
Echo ...

if %errorlevel% == 0 (
    echo ....you're all good bro.
) else (
    ping -n 3 "8.8.8.8" | findstr /r /c:"[0-9] *ms"
    if ERRORLEVEL 1 IF NOT ERRORLEVEL 2 (
echo ....all is lost
)
)
Echo.
GOTO :EOF

The 'for /f tokenises the output of the command in single-quotes using : as a delimiter and picking the second token to apply to %%a`.

The | needs to be escaped by a caret to tell cmd that it is part of the command to be executed, not part of the for .

The for processes the output of ipconfig... so %%a has the form Spacexxx.xxx.xxx.xxx

On my machine, xxx... was missing on one line, so I filtered out the single-space response.

Pinging %%a without the quotes adds an extra space into the ping line, which is harmless. With the quotes, it didn't like it.

Then the appendix...

I'm not sure what you're really attempting here. Perhaps it was a frustrated debug attempt.

errorlevel will be set to whatever the last ping result was. This is logically unpredictable.

I've shown a correct method of interpreting the findstr errorlevel result. I'm not sure that your regex is correct here...but the all is lost message would only be shown if findstr sets errorlevel to 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM