Ping a user defined IP-Range with batch

霸气de小男生 提交于 2019-12-08 09:45:31

问题


I'm working on a school project and I wanted to create a batch file that pings a user-defined IP-range and writes the successful pings to a txt file.

Now I'm stuck at the FOR loop:

@echo off
SET /p IPRange=Bitte IP-Range eingeben(xxx.xxx.xxx) :
SET /p AnfangsIP=Bitte AnfangsIP des Bereiches eingeben :
SET /p EndIP=Bitte EndIP des Bereiches eingeben :

FOR /L %IP% (%AnfangsIP%,1,%EndIP%) DO (
    ping -n 1 %IP-Range%.%IP% | find "TTL=" >nul
    if errorlevel 1 (
        echo %IP-Range%.%IP% not reachable
    ) else (
        echo %IP-Range%.%IP% reachable
    )
)

pause>nul

The batch file is executed with these parameters: IP range, start IP-address, end IP-address. My goal is to make a for loop that increments the variable %IP% every loop. From the start IP-address to the end IP-address.

I searched in the forum but there are just single pings not for a whole range of IPs.


回答1:


Try this:

@echo off

SET /p IPRange=Bitte IP-Range eingeben(xxx.xxx.xxx) :
SET /p AnfangsIP=Bitte AnfangsIP des Bereiches eingeben :
SET /p EndIP=Bitte EndIP des Bereiches eingeben :

FOR /L %%I IN (%AnfangsIP%,1,%EndIP%) DO (
    ping -n 1 %IPRange%.%%I | find "TTL=" >nul
    if errorlevel 1 (
        echo %IPRange%.%%I not reachable
    ) else (
        echo %IPRange%.%%I reachable
    )
)

pause>nul

EDIT: Output example added

C:\> test.bat
Bitte IP-Range eingeben(xxx.xxx.xxx) :127.0.0
Bitte AnfangsIP des Bereiches eingeben :0
Bitte EndIP des Bereiches eingeben :7
127.0.0.0 not reachable
127.0.0.1 reachable
127.0.0.2 reachable
127.0.0.3 reachable
127.0.0.4 reachable
127.0.0.5 reachable
127.0.0.6 reachable
127.0.0.7 reachable



回答2:


He Guys

Thanks a lot for the answers. Finally the script of Aacini works. In time i made a script without any for loop that isn't that beautiful...

Here's the Code:

@ECHO off
COLOR F0

:START
BREAK>PINGLOG.txt

CLS
SET /P range=Bitte IP-Range eingeben(xxx.xxx.xxx):
CLS
SET /P sIP=Bitte Start-IP eingeben(xxx):
CLS
SET /P eIP=Bitte End-IP eingeben(xxx):
CLS

SET /P JN1=START PING FROM %range%.%sIP% - %range%.%eIP%?(Y/N):

CLS
SET /A counter=%sIP%
SET /A endIP=%eIP%
SET /A endIP+=1
CLS

IF /I %JN1%==Y (
    GOTO PINGRANGE
) ELSE (
    GOTO ENDE2
)

:PINGRANGE
COLOR 7C
PING -n 1 %range%.%counter% | find "TTL=" >nul
IF NOT ERRORLEVEL 1 (
    ECHO ------------------------------------------------- >>PINGLOG.txt
    ECHO %range%.%counter% is reachable! >>PINGLOG.txt
    ECHO %range%.%counter% is reachable!
) ELSE (
    ECHO %range%.%counter% is not reachable!
)

SET /A counter+=1
GOTO CHECK
)

:CHECK
IF %counter%==%endIP% (
    GOTO ENDE1
) ELSE (
    GOTO PINGRANGE
)

:ENDE1
COLOR F0
ECHO -------------------------------------------------
SET /P JN2= FINISH! WOULD YOU SEE THE LOGFILE?(Y/N):
IF /I %JN2%==Y (
    START NOTEPAD "PINGLOG.txt"
) ELSE (
    EXIT cmd.exe
)

:ENDE2
COLOR F0
ECHO -------------------------------------------------
SET /P JN3= NEW IP-RANGE PING?(Y/N):
IF /I %JN3%==Y (
    GOTO START
) ELSE (
    EXIT cmd.exe
)

I now that this is not very beautiful :)

Have A Nice Day Lukas



来源:https://stackoverflow.com/questions/32572939/ping-a-user-defined-ip-range-with-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!