解析并附加ip和子网掩码地址

时间:2015-03-12 06:53:42

标签: batch-file

我正在尝试解析,附加ip和子网掩码地址。解析工作正常但附加会给我带来问题。 在这里,我正在粘贴我的代码。基本上我正在获取IP和子网掩码,然后做一些掩码然后追加。

假设我有以下内容,

IPADDR : 10.239.230.47
SUBNET : 255.255.254.0

我得到这样的输出:

-----------------------------------------------------------------
255.255.254.0
"10.239.231                                                     .255"
Press any key to exit
-----------------------------------------------------------------------

我无法解决为什么我得到空间btw.Please帮助我。

  @ECHO OFF
    ECHO.
    set dot=.
    set "a1=0"
    set "b1=0"
    set "c1=0"
    set "d1=0"

    set "a2=0"
    set "b2=0"
    set "c2=0"
    set "d2=0"
    set "temp1=0"
    set "temp2=0"

    set "mask=0x01"
    set "_result=0"
    set "_IPADDR1=0"

    FOR /F "TOKENS=2* DELIMS=:" %%A IN ('IPCONFIG ^| FIND "IPv4"') DO FOR %%B IN (%%A) DO SET IPADDR=%%B
    FOR /f "tokens=1,2,3,4 delims=." %%a IN ("%IPADDR%") do set a1=%%a&set b1=%%b&set c1=%%c&set d1=%%d


    FOR /F "TOKENS=2* DELIMS=:" %%A IN ('IPCONFIG ^| FIND "Subnet Mask"') DO FOR %%B IN (%%A) DO SET SUBNET=%%B
    FOR /f "tokens=1,2,3,4 delims=." %%a IN ("%SUBNET%") do set a2=%%a&set b2=%%b&set c2=%%c&set d2=%%d


    echo %SUBNET%

    IF %c2% EQU 255  set temp1=1
    IF %c2% EQU 0    set temp1=1

    IF %temp1% EQU 1 (
    set c1=255
    )ELSE (
    :loop1
        set /a "_result=c2&mask"
        IF %_result% GTR 1 (
        set c1=%c1% 
        )ELSE (
        set /a "c1=c1|mask"
        )

        IF %mask% EQU 128 (
        goto:exit_loop1
        )ELSE (
        set /a "mask=mask<<1"
        goto:loop1
        )

    )
    :exit_loop1

    set "mask=0x01"
    set "_result=0"
    IF %d2% EQU 255  set temp2=1
    IF %d2% EQU 0    set temp2=1

    IF %temp2% EQU 1 (
    set d1=255
    )ELSE (
    :loop2
        set /a "_result=d2&mask"
        IF %_result% GTR 1 (
        set d1=%d1% 
        )ELSE (
        set /a "d1=d1|mask"
            )

        IF %mask% EQU 128 (
        goto:exit_loop2
        )ELSE (
        set /a "mask=mask<<1"
        goto:loop2
        )
    )
    :exit_loop2

    set  _IPADDR1="%a1%%dot%%b1%%dot%%c1%%dot%%d1%"
    echo.%_IPADDR1%

    SET /p exit=Press any key to exit
    :End

1 个答案:

答案 0 :(得分:0)

set c1=%c1% 
           ^ There is a space here being added

最好使用

set "c1=%c1%"

将避免此问题