【问题标题】:Batch file to print URL and IP list for hosts file用于打印主机文件的 URL 和 IP 列表的批处理文件
【发布时间】:2016-03-24 14:03:41
【问题描述】:

我在一个 txt 文件(比如 URLList.txt)中有一个主机/url 列表,我需要获取所有这些文件的 IP 列表。 有很多 url,所以手动完成会很长。 基本上我想知道,根据输入文件列表,我是否可以得到这样的结果:

host1 IP1
host2 IP2
.
.
hostn IPn

例如

s12web 120.234.567.12
s34web 12.444.32.22

然后我会把这个输出复制到我的主机文件中

非常感谢您的大力帮助,

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    您可以使用它来做到这一点,感谢@Lizz for the way to find the ip

    @echo off
    for /f "usebackq delims=" %%h in ("URLList.txt") do (
        for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
            echo.%%h %%f
        )
    )
    pause
    

    并将其回显到文件中,您可以使用:

    @echo off
    type nul>IPList.txt
    for /f "usebackq delims=" %%h in ("URLList.txt") do (
        for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
            echo.%%h %%f
        )>>IPList.txt
    )
    pause
    

    正如@aschipfl 指出的,另一种更优雅的解决方案:

    @echo off
    >IPList.txt (
    for /f "usebackq delims=" %%h in ("URLList.txt") do (
        for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
            echo.%%h %%f
        )
    )
    )
    

    【讨论】:

    • 如果将整个for 循环放在括号之间并通过> 重定向其输出一次,则无需初始化空文件...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多