【问题标题】:How to get lines that start with specific word in cmd from a text file?如何从文本文件中获取以 cmd 中特定单词开头的行?
【发布时间】:2021-12-31 05:18:25
【问题描述】:

我试图用数据包跟踪器模拟真实网络。因此,我获取了特定域中的网站 URL,并将它们逐行放入文件名为 WebSites-DB 的 ASCII 文件中,如下所示:

www.google.com
stackoverflow.com

然后我使用命令ping获取他们的IP地址:

FOR /F "eol=#" %i in (WebSites-DB) do ping %1 -n 1 -w 1 1>> Ips.txt

但是ping输出多行:

Pinging www.google.com [8.8.8.8] with 32 bytes of data:
Request timed out.

Ping statistics for 185.216.132.250:
    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),

我只想要网站名称www.google.com 及其IP 地址8.8.8.8

如何只获取感兴趣的数据?

【问题讨论】:

  • ping 工具不会“模拟真实的网络流量”,因为它会发出 IGMP 数据包,路由器和防火墙等网络设备对这些数据包的处理方式与普通 Web HTTPS/TCP 数据包不同。
  • 我知道....我只是用它来获取ips....
  • 要解析主机名,您应该使用dignslookup 而不是ping

标签: cmd ping


【解决方案1】:

使用这一行:

FOR /F "usebackq tokens=2,3 delims= " %i in (`find "Pinging" Ips.txt`) do @Set "_=%j"&Call Echo %i ^%_:~1,-1^%

它会给出如下结果:

www.goog.com 8.8.8.8

更多信息:
substring windows command line

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    相关资源
    最近更新 更多