【问题标题】:Batch script to get website ip address?获取网站IP地址的批处理脚本?
【发布时间】:2011-04-21 16:04:27
【问题描述】:

我正在尝试整理一个批处理文件,它将 ping 一个网站,并将其 ip 分配给一个变量 - 我已经搜索过,但还没有真正能够将一些东西放在一起。谁能把我推向正确的方向。

提姆。

【问题讨论】:

    标签: windows batch-file ip-address


    【解决方案1】:

    你可以试试 ping 命令。 这个想法是获取 ping 输出的 [] 之间的部分。

    @echo off
    setlocal EnableDelayedExpansion
    set myServer=google.de
    
    for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (
     if "%%b" NEQ "" set ip=%%b
    )
    echo ip is %ip%
    

    【讨论】:

    • 最好使用ping -n 1 -w 0 !myServer!,无需等待。
    • -w 0 似乎只有在 -n 大于 1 时才会生效
    【解决方案2】:

    如果您只想查找地址,您可能希望使用nslookup 而不是ping。搜索“nslookup batch”会给你一堆结果,包括this 一个看起来应该很容易适应的结果,因为它将结果存储在变量中。

    【讨论】:

      【解决方案3】:

      与上面的@jeb 答案相同,但不使用EnableDelayedExpansion,只需将“www.google.com”替换为您喜欢的网站或%variablename%

      for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 www.google.com ^|find /i "pinging"') do echo IP=%f
      

      【讨论】:

        猜你喜欢
        • 2018-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多