1.

Solve : Script that executes steps (takes action) if IP address is correct?

Answer»

I wrote a script that installs program updates based on a number of criteria including which DATA center the server is located in. However the script doesn't SEEM to determine the data center properly.

The following line is supposed to check the first two octets of the IP address and if they are found goto the "ProxyOff" section of the script to disable the proxy server settings so the Internet is accessible.

Code: [Select]ipconfig | findstr "10.64" & IF %ERRORLEVEL% == 1 GOTO PROXYOFF
I noticed that this wasn't running reliably so I rewrote the above for testing:

Code: [Select]ipconfig | findstr "10.64" & echo %ERRORLEVEL%
Whether I use find or findstr the results are the same.
Whether I use 10.64, 10.14, 10.44, etc. the error level is always the same.

The expected result is:

If the ip address starts with 10.64 goto Proxyoff
If the ip address starts with 10.14 goto Proxyoff
If the ip address starts with 10.44 goto Proxyon

What do I need to change for find or findstr error level to play nice?

Thanks for the help,

MJ

(1) The errorlevel from a command is only available after the line has run. You can't capture it on the same line using &, unless you use delayed expansion.
(2) The errorlevel after a successful find is 0 (zero).

This does not show the errorlevel correctly:

wrong.bat

echo off
setlocal enabledelayedexpansion
echo find "%2" in "%1"
echo %1 | findstr "%2" >nul & echo errorlevel=%errorlevel%


This shows the errorlevel correctly:

right1.bat

@echo off
echo find "%2" in "%1"
echo %1 | findstr "%2" >nul
echo errorlevel=%errorlevel%


This also shows the errorlevel correctly:

right2.bat

@echo off
setlocal enabledelayedexpansion
echo find "%2" in "%1"
echo %1 | findstr "%2" >nul & echo errorlevel=!errorlevel!

C:\>wrong.bat catalog cat
find "cat" in "catalog"
errorlevel=0

C:\>wrong.bat catalog dog
find "dog" in "catalog"
errorlevel=0

C:\>right1.bat catalog cat
find "cat" in "catalog"
errorlevel=0

C:\>right1.bat catalog dog
find "dog" in "catalog"
errorlevel=1

C:\>right2.bat catalog cat
find "cat" in "catalog"
errorlevel=0

C:\>right2.bat catalog dog
find "dog" in "catalog"
errorlevel=1

You can however use if errorlevel

This will check the errorlevel correctly but it will also match 201.123.110.643

Code: [Select]ipconfig | findstr "10.64" & IF ERRORLEVEL 1 GOTO PROXYOFF
You need to parse the entire IP address and then check for the *first* two octets with something like this

Code: [Select]echo %ip% | findstr /r "^10\.64\." & IF ERRORLEVEL 1 GOTO PROXYOFF Another approach would be to leave out explicitly checking the errorlevel and letting the code implicitly DECIDE if the IP mask was found.

Code: [Select]@echo off
setlocal

ipconfig | findstr /r " 10\.64\." > nul && goto proxyoff
ipconfig | findstr /r " 10\.14\." > nul && goto proxyoff
ipconfig | findstr /r " 10\.44\." > nul && goto proxyoff
::
:: fallthru logic GOES here
::
goto :eof

:proxyoff
::
:: proxyoff logic goes here
::

The redirection to nul keeps things neat, but you can remove it.

My 2¢ 10¢ worth QUOTE from: Sidewinder on May 21, 2013, 05:45:46 AM

My 2¢ 10¢ worth

I'd say that was worth a couple of dollars at least, SW.

10.44.xxx.xxx should go to proxyon though...





Salmontrout, THANK YOU!

That did the trick. Was able to test this morning and everything worked as it should. Is it possible to do something like this:

If Errorlevel 0 in (ipconfig | findstr "10.64" > NUL) GOTO ?

I just refreshed my screen and saw more replies. I'm interested in what Sidewinder posted too. What does Code: [Select]&& do?


Thanks again,

MJQuote
What does && do?

Do you want to handle that, SW?
Quote
Do you want to handle that, SW?

Haven't got a clue


The symbol && is used to execute the code to the right of the symbol if the instruction immediately preceding the symbol is true.

Code: [Select]ipconfig | findstr /r " 10\.64\." > nul && goto proxyoff

In this case the goto is executed if the findstr instruction is true, meaning the IP pattern (" 10\.64\.") has been found.

If you are interested, there is an inverse notation || , which is used to execute the code to the right of the symbol if the instruction immediately preceding the symbol is false.

Put another way, the && and || operator are errorlevel tests.

command1 && command2
this line runs command1 and if the errorlevel from command1 is equal to 0 (zero), then it runs command2.

command1 || command2
this line runs command1 and if the errorlevel from command1 is NOT equal to 0 (zero), then it runs command2.
(note: "not equal to zero" - not just greater than zero; less than zero also. Some programs return negative errorlevels.)

You can do stuff with brackets as well

command1 && (
echo no error
some other code to execute if there was no error
...etc
)

You can combine && and ||
if you do this put the && test first.

command1 && (echo no error) || (echo an error)

command1 && (
echo no error
echo That's right, no error
) || (
echo an error
echo Yup, an error
)

In fact with a little thought you can do away with GOTOs and labels.



http://judago.webs.com/batchoperators.htmnice pageI use FOR with the netsh command to find a host's own IP address and then it can be used again to isolate the first two octets

@echo off
set mycommand=netsh interface ip show addresses "Local Area Connection"
for /f "tokens=1-3 delims= " %%A in ('%mycommand% ^| find "IP Address:"') do set hostIP=%%C
for /f "tokens=1-4 delims=." %%A in ("%hostIP%") do set first2octets=%%A.%%B
if "%first2octets%"=="10.64" goto Proxyoff
if "%first2octets%"=="10.14" goto Proxyoff
if "%first2octets%"=="10.44" goto Proxyon
This is fantastic! Excellent information. Thank you everyone!Quote from: Salmon Trout on May 22, 2013, 12:26:31 PM
I use FOR with the netsh command to find a host's own IP address and then it can be used again to isolate the first two octets

@echo off
set mycommand=netsh interface ip show addresses "Local Area Connection"
[snip]

That relies on the network adapter name and in my case it gives the Virtualbox IP address, and not my internet connection.

If the OP wants to get his internet facing IP address then this is one way, relying on an external website to report the information.

Code: [Select]@echo off
>"%temp%\ip.vbs" echo Set objHTTP = CreateObject("MSXML2.XMLHTTP")
>>"%temp%\ip.vbs" echo Call objHTTP.Open("GET", "http://astronomy.comoj.com/ip.php", False)
>>"%temp%\ip.vbs" echo objHTTP.Send()
>>"%temp%\ip.vbs" echo wscript.echo objHTTP.ResponseText

for /f "delims=" %%a in ('cscript /nologo "%temp%\ip.vbs"') do (
if not defined ip set ip=%%a
)
echo %ip%
pause




Discussion

No Comment Found