Batch Random Pinging(尝试确定ping是否成功)

Batch Random Pinging(Trying to determine if a ping is successful)

我制作了一个程序来创建随机 IP 并 ping 它们以查看它们是否存在。它使用“>”和“>>”记录输出。我想检查 ping 是否超时以及它是否确实将其从文本文档中排除。到目前为止,这是我的代码:

`@ECHO OFF
:LOOP
 SET /A N1=%RANDOM% * 255 / 32768
 SET /A N2=%RANDOM% * 255 / 32768
 SET /A N3=%RANDOM% * 255 / 32768
 SET /A N4=%RANDOM% * 255 / 32768
 PING %N1%.%N2%.%N3%.%N4%>>"%USERPROFILE%\Desktop\IP.txt"
 GOTO LOOP`

ping 完成后,我希望它记录响应的 IP,而不是超时的 IP。

-提前致谢

@echo off
setlocal EnableDelayedExpansion

rem Set the number of IP's to test
set num=20

echo Creating and testing %num% IP's
echo/

for /L %%i in (1,1,%num%) do (

   rem Create the random IP
   set /A N1=!random! %% 255, N2=!random! %% 255, N3=!random! %% 255, N4=!random! %% 255
   set "IP=!N1!.!N2!.!N3!.!N4!"

   rem Show the IP and leave the cursor after it
   set /P "=%%i- Testing !IP!: " < NUL

   rem Test the IP with ping and get just the 3rd word of the 3rd line from ping output
   set "word="
   for /F "skip=2 tokens=3" %%a in ('ping !IP!') do if not defined word set "word=%%a"

   rem If that word is not "out", the IP is correct
   if "!word!" neq "out" (
      echo CORRECT
      echo !IP!>> IP.txt
   ) else (
      echo failed...
   )

)
@echo off
    setlocal enableextensions enabledelayedexpansion

    for /l %%a in (0) do (
        set /a  "A=!random! %% 255", ^
                "B=!random! %% 255", ^
                "C=!random! %% 255", ^
                "D=!random! %% 255"

        ping -w 1000 -n 1 "!A!.!B!.!C!.!D!" | find "TTL=" > nul && (
            >>"online.txt" echo !A!.!B!.!C!.!D!
        )
    )

这会创建一个无限循环(这个 for /l %%a in (0) 意味着 for %%a starting in 0 up to 0 in steps of 0

对于每次迭代,都会为每个 ip 地址八位字节生成四个随机数。为此,我们使用 !random! 变量中的构建生成一个随机数(需要 delayed expansion),然后除以 255(模运算符在批处理文件中为 %%),使用一个 set /a 命令,进行计算的批处理方式。

一旦生成了 ip(更延迟的扩展,请参阅之前链接的答案),将发送一个 ping 并检查其输出是否存在 TTL= 字符串 (more information here)。如果存在此字符串,则目标是可访问的,地址将附加到文件中。

为了测试字符串是否出现在 ping 命令的输出中,它被输送到搜索指定字符串的 find 命令中。如果找到字符串 errorlevel 变量设置为 0,如果没有找到,errorlevel 将设置为 1。

使用条件执行运算符 && 检查此值。这意味着 如果前一个命令没有将 errorlevel 设置为任何大于 0 的值,则执行下一个命令

所以,如果find命令找到字符串,errorlevel将不是1,echo命令将被执行。此 echo 被重定向以追加到目标文件(>> 是追加重定向)