Batch file WINDOWS提示从文件获取网页的IP
我必须获得我将从txt文件传送的网页的IP: 例如,txt文件如下所示:Batch file WINDOWS提示从文件获取网页的IP,batch-file,ip,ping,prompt,Batch File,Ip,Ping,Prompt,我必须获得我将从txt文件传送的网页的IP: 例如,txt文件如下所示: google.com yahoo.com toyota.com bmw.com etc... 我得买点像这样的东西 81.177.116.172 11.127.114.122 etc.. or 81.177.116.172 - google.com 11.127.114.122 - yahoo.com 我知道我可以用 ping websiteurl.com > ping.txt 但我想检查2000页左右。请建
google.com
yahoo.com
toyota.com
bmw.com
etc...
我得买点像这样的东西
81.177.116.172
11.127.114.122
etc..
or
81.177.116.172 - google.com
11.127.114.122 - yahoo.com
我知道我可以用
ping websiteurl.com > ping.txt
但我想检查2000页左右。请建议如何以最快的方式完成,以及如何将txt文件中的行作为参数传递给ping。谢谢下一步更新和评论代码片段比我原始答案中的代码快:
@ECHO OFF
SETLOCAL enableextensions disabledelayedexpansion
set "_format=%~1" use for output ''beautifying''
if defined _format (
echo hostname OP IPv4_address explanation
echo -------- -- ------------ -----------
)
set "_file=files\30852528.txt" change to fit your circumstances
for /F "usebackq delims=" %%i in ("%_file%") do (
set "_host=%%i" remember input line
for /F "tokens=1 delims==" %%G in ('set ___ping 2^>NUL') do set "%%G="
set /A "_line=0"
for /F "delims=" %%G in ('ping -a -4 -n 1 %%i') do (
set /A "_line+=1"
call set "___ping%%_line%%=%%G" remember ping output to an array-like variable
)
REM rem debug output in next two lines
REM echo(
REM for /F "tokens=1* delims==" %%G in ('set ___ping 2^>NUL') do echo(%%H
call :pings
)
ENDLOCAL
goto :eof
:pings
set "_operator=??"
set "_hostIPno=%_host%"
set "_hostName=%_host%"
set "_auxiliar="
set "_explains=Unknown reason" default values set
set "_string=Ping request could not find host"
for /F "delims=" %%G in ('set ___ping^|findstr /i /C:"%_string%"') do (
set "_operator=##"
set "_explains=%_string%" ping request could not find host
goto :pingsDone
)
set "_string==Pinging"
for /F "tokens=1-4 delims==[] " %%G in ('set ___ping^|findstr /i /C:"%_string%"') do (
echo("%%J"|findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*">NUL
if errorlevel 1 (
set "_auxiliar=%%H %%I"
) else (
set "_auxiliar=%%H %%I [%%J]"
set "_hostIPno=%%J"
set "_hostName=%%I" address to hostname resolved
)
)
set "_string=Destination host unreachable"
for /F "tokens=1* delims==" %%G in ('set ___ping^|findstr /i /C:"%_string%"') do (
set "_explains=%%H"
set "_operator=?=" destination host unreachable
)
set "_string=Request timed out"
for /F "tokens=1* delims==" %%G in ('set ___ping^|findstr /i /C:"%_string%"') do (
set "_explains=%_auxiliar%: %%H"
set "_operator==?" request timed out
)
set "_string=TTL="
for /F "tokens=1* delims==" %%G in ('set ___ping^|findstr /i "%_string%"') do (
set "_explains=%%H"
set "_operator===" ping request successful
)
:pingsDone
rem basic formatting: output to columns only for demonstration at StackOverflow
if defined _format (
set "_hostName= %_hostName%"
set "_hostIPno=%_hostIPno% "
set "_explains= %_explains%"
) else set "_explains="
if defined _format (
set "_hostName=%_hostName:~-15%"
set "_hostIPno=%_hostIPno:~0,15%"
)
rem output with delayed expansion enabled:
SETLOCAL enabledelayedexpansion
echo(!_hostName! !_operator! !_hostIPno!!_explains!
ENDLOCAL
goto :eof
数据:
d:\bat> type "files\30852528.txt"|findstr /V "^;"
foo.bar
google.com
77.75.79.53
192.168.1.1
192.168.1.12
bmw.com
160.46.244.131
d:\bat> so\30852528.bat 1
hostname OP IPv4_address explanation
-------- -- ------------ -----------
foo.bar ## foo.bar Ping request could not find host
google.com == 216.58.209.206 Reply from 216.58.209.206: bytes=32 time=24ms TTL=56
www.seznam.cz == 77.75.79.53 Reply from 77.75.79.53: bytes=32 time=10ms TTL=248
192.168.1.1 == 192.168.1.1 Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
192.168.1.12 ?= 192.168.1.12 Reply from 192.168.1.100: Destination host unreachable.
bmw.com =? 160.46.244.131 Pinging bmw.com [160.46.244.131]: Request timed out.
origin.bmw.com =? 160.46.244.131 Pinging origin.bmw.com [160.46.244.131]: Request timed out.
输出(从打开的cmd
窗口运行):
美化输出:
d:\bat> type "files\30852528.txt"|findstr /V "^;"
foo.bar
google.com
77.75.79.53
192.168.1.1
192.168.1.12
bmw.com
160.46.244.131
d:\bat> so\30852528.bat 1
hostname OP IPv4_address explanation
-------- -- ------------ -----------
foo.bar ## foo.bar Ping request could not find host
google.com == 216.58.209.206 Reply from 216.58.209.206: bytes=32 time=24ms TTL=56
www.seznam.cz == 77.75.79.53 Reply from 77.75.79.53: bytes=32 time=10ms TTL=248
192.168.1.1 == 192.168.1.1 Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
192.168.1.12 ?= 192.168.1.12 Reply from 192.168.1.100: Destination host unreachable.
bmw.com =? 160.46.244.131 Pinging bmw.com [160.46.244.131]: Request timed out.
origin.bmw.com =? 160.46.244.131 Pinging origin.bmw.com [160.46.244.131]: Request timed out.
d:\bat>so\30852528.bat 1
主机名操作IPv4\u地址说明
-------- -- ------------ -----------
foo.bar##foo.bar Ping请求找不到主机
google.com==216.58.209.206来自216.58.209.206的回复:字节=32时间=24ms TTL=56
www.seznam.cz==77.75.79.53从77.75.79.53回复:字节=32时间=10ms TTL=248
192.168.1.1==192.168.1.1从192.168.1.1回复:bytes=32 time我知道这是PowerShell对批处理问题的回答,但我发现很多人在真正想要命令行时会问如何批处理。如果您想学习如何做这样的事情,请学习PowerShell而不是batch,因为它比复杂的batch FOR command更容易学习(我向真正理解它的人表示歉意)
…并且不确定您到底想要什么--这将查找名称并ping(第一个返回的)IP地址。如果您想先将IP地址写入文本文件,然后读取并ping它们,这没什么大不了的,但我不认为这样做有什么意义,因为IP可能在任何给定时间发生更改,然后您需要解析名称并再次将新IP写入文件。