Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

batch file with arithmatic operations on ip addresses

msheekhahmsheekhah Member Posts: 1
i'm trying to write a program for my company to detect what kind of network a client is on. our product is designed to work with a router, meaning they have a 192.168.x.x network. I wrote code for a dos batch file to do that, but then found out that > and < are not arithmetic operators in dos. Can someone help?

IPCONFIG |FIND "IP" > %temp%TEMPIP.txt
FOR /F "tokens=2 delims=:" %%a in (%temp%TEMPIP.txt) do set IP=%%a
del %temp%TEMPIP.txt
set IP=%IP:~1%
set 192=0
set 10=0
set live=0
echo %IP% >%temp%ip.txt
echo The current IP address is "%IP%"

pause

IF %IP% < 192.168.255.255 %192% = %192% +1
IF %IP% > 192.168.0.1 %192% = %192% +1

IF %IP% < 10.255.255.255 %10% = %10% +1
IF %IP% > 10.0.0.1 %10% = %10% +1

IF NOT %192% == 2 %live% = %live% + 1
IF NOT %10% == 2 %live% = %live% + 1

IF %192% == 2 ECHO THIS IS A VALID ROUTER.

IF %10% == 2 ECHO THIS IS A VALID ROUTER BUT NEEDS TO BE IN THE 192

SUBNET.

IF %live% == 2 ECHO THIS IS NOT A ROUTER. IT IS EITHER A HUB, GATEWAY, OR

SWITCH.

pause

Comments

  • KarmashockKarmashock Member Posts: 2
    First, I'll tell you what little I know about this:

    If you want to say less then or more then in IF statements you have to use

    EQU ( Same as = )
    NEG ( Same as =/= )
    LSS ( Same as < )
    LEQ ( Same as </= )
    GTR ( Same as > )
    GEQ ( Same as =/> )

    You can use == instead of EQU but you're less liable to make mistakes if you avoid that expression and stick with EQU (or at least I am). Half the time when I start doing that I end up saying %var1%=%var2% which crashes the script... so I stick with EQU.

    I've been trying to figure out this same problem all day. I've been working on a quick and dirty network diagnostic script that I can launch from a USBkey or email someone.


    The script is really pretty much perfect. It's pretty 'smart' for an 8k batch file. The only problem with it is that I haven't figured out how to get it to capture the gateway address.


    The closest I've come is something like

    ipconfig | findstr "Gateway"


    or


    tracert -h 1 | findstr "ms"

    I'm using findstr here instead of find because it has more robust features and I'm assuming I'll need them.


    The problem is both of these commands are not refined enough. You get a lot of junk output along with the number.

    The first method outputs something like this:

    Default Gateway . . . . . . . . . :
    Default Gateway . . . . . . . . . :
    Default Gateway . . . . . . . . . : 192.168.1.1
    Default Gateway . . . . . . . . . :

    and the second method outputs something like this:

    1 1 ms <1 ms <1 ms 192.168.1.1


    What we both need unless there is a better way... is a way to filter just the IP number out of the output. Then we can pipe it into a variable or whatever.
  • PhylumPhylum Member Posts: 1
    My two cents

    I'm thinking this process will need to be broken down into chunks in order to do what needs to be done.

    So first we'll need the gateway

    :: Get the Gateway
    for /F "usebackq tokens=13" %%i in (`ipconfig ^| find /i "gateway"`) do set gateway=%%i

    You can use a similar technique for getting the IP address
    for /F "usebackq tokens=14" %%i in (`ipconfig ^| find /i "IPv4"`) do echo %%i

    (Note: I was trying to make use of the 'delims' option but I wound up with extra spaces I didn't want to have to deal with trimming. For example, the following would return " 192.168.1.2" instead of "192.168.1.2"

    for /F "usebackq tokens=2 delims=:" %i in (`ipconfig ^| find /i "IPv4"`) do echo %i

    or

    for /F "usebackq tokens=13 delims=: " %i in (`ipconfig ^| find /i "IPv4"`) do echo %i

    Any ideas on overcoming this?)

    Next I'm thinking you'll probably need to split the IP at some point; I recommend using a subroutine of sorts.

    CALL :SPLITIP %gateway%

    :SPLITIP
    :: Split IP into Separate Octects
    set IP=%1
    if defined IP (
    for /F "usebackq delims=. tokens=1-4" %%i in (`echo %IP%`) do (
    set GIPO1=%%i
    set GIPO2=%%j
    set GIPO3=%%k
    set GIPO4=%%l
    )
    ) else ( echo ERROR: Gateway Not Defined (%gateway%) & ping -n 30 localhost >nul & goto END )
    rem for %%i in (%GIPO1% %GIPO2% %GIPO3% %GIPO4%) do echo %%i
    GOTO :EOF

    At at some point you may need to know the length of a variable or two in order to do some advanced string manipulation. I use yet another subroutine for that.
    @echo off
    cls

    set mystring=five

    CALL :GETLENGTH %mystring%

    :GETLENGTH
    :: Send the length of the variable %1 to the variable %var_length%
    :: Thanks to Leonardo Pignataro secret_doom@hotmail.com www.batch.hpg.com.br
    set length_target=%1
    set var_length=0
    :GLoop
    if defined length_target (
    set length_target=%length_target:~1%
    set /A var_length += 1
    goto GLoop
    )
    echo Var is %var_length% characters long!
    GOTO :EOF

    This should return "Var is 5 characters long!"

    Haven't throughly tested any of this code, but it should be good.
Sign In or Register to comment.