Reputation: 331
I've the following batch file to check the IP range the computer is in to decide whether or not the ABC.exe needs to be launched.
This works fine and since the first part of all 3 office locations share the same numbers at the beginning of their IP ranges up till the first number of the third octet (10.41.1) I'm currently just check for that.
@ECHO OFF
SET OFFICE_ALL=10.41.1
REM SET OFFICE_1=10.41.168.
REM SET OFFICE_2=10.41.162.
REM SET OFFICE_3=10.41.172.
REM SET VPN_CONNECTION=10.36.
ipconfig | find /i " %OFFICE_ALL%" >nul 2>nul && (
start C:\ABC-tool\ABC.exe
) || (
exit
)
That all said, we're going to move to a new location and the network colleagues told me that the IP range for the new office will become 10.44.121.x.
I've tried to adjust my script like this:
@ECHO OFF
SET OFFICE_MOST=10.41.1
REM SET OFFICE_1=10.41.168.
REM SET OFFICE_3=10.41.172.
SET OFFICE_2=10.44.121.
REM SET VPN_CONNECTION=10.36.
ipconfig | find /i " %OFFICE_MOST%" >nul 2>nul && (
start C:\ABC-tool\ABC.exe
) || ipconfig | find /i " %OFFICE_2%" >nul 2>nul && (
start C:\ABC-tool\ABC.exe
) || (
exit
)
But somehow that doesn't work... My understanding of the || is that the code after it only gets executed when the code prior to it wasn't successful, so it basically acts like an if-else construction. But apparently there is more to it and I can't figure it out. Where and why do I go wrong in my adjusted code?
Upvotes: 0
Views: 225
Reputation:
I suggest you better parse ipconfig output to store the IPv4 in a variable
and the dot separated octets split into their own variables.
:: Q:\Test\2018\06\27\SO_51066473.cmd
@Echo off&SetLocal EnableDelayedExpansion
For /f "tokens=2delims=:" %%I in (
'ipconfig ^| findstr "[^n].IPv4.Add.*:"'
) Do (
Set "IPv4=%%I"
For /f "tokens=1-4delims=. " %%A in ("%%I"
) do set /A "OC1=%%A,OC2=%%B,OC3=%%C,OC4=%%D"
)
Set "IPv4=%IPv4:~1%"
Echo Your IPv4 Address is : [%IPv4%]
Echo the Octets are:
For /l %%L in (1,1,4) Do Echo=OC%%L=!OC%%L!
So you are able to do your comparisons on all or part of the numbers.
Sample output:
> SO_51066473.cmd
Your IPv4 Address is : [192.168.45.183]
the Octets are:
OC1=192
OC2=168
OC3=45
OC4=183
Upvotes: 1