在ping输出中将IP与文本分开?

编程入门 行业动态 更新时间:2024-10-28 11:31:28
本文介绍了在ping输出中将IP与文本分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望能够运行一个批处理文件,该文件将找到连接到路由器的每个设备的IP地址,然后从中查找其主机名.到目前为止,我的情况如下:

I'd like to be able to run a single batch file that would find the IP address of every device connected to my router, and then find their hostnames from it. What I have so far is below:

@echo off set /a n=0 :repeat set /a n+=1 echo 192.168.1.%n% ping -n 1 -w 500 192.168.1.%n% | FIND /i "Reply">>devices.txt if %n% lss 254 goto repeat type devices.txt goto :hostname

这将找到所有可能的地址,并将它们写入文本文件devices.txt. 但是,我现在得到以下结果:

This finds all the possible addresses and writes them to a text file, devices.txt. However, I now end up with the following:

Reply from 192.168.1.82: bytes=32 time=5ms TTL=255

批处理文件的下一部分是:

The next part of the batch file goes:

:hostname ping -a %ip% -l 1 -n 1 >> hostnames.txt

这会ping在devices.txt中找到的IP,并在hostnames.txt中返回结果-至少,我希望这样做.

This pings the IPs found in devices.txt and returns the result in hostnames.txt - or at least, I'd like it to.

在将IP地址写入devices.txt时,我需要以某种方式将IP地址与其他文本分开,然后将其分配为变量,以便下一部分可以使用它.

Somehow I need to separate the IP address from the other text when writing it to devices.txt, and then assign it as a variable so it can be used by the next part.

我可以用一个简单的功能来做这两个事情吗?

Is there a simple function I can use to do either of these things?

推荐答案

尝试一下:

@echo off setlocal set "p=ping -n 1 -w 500 192.168.1" for /l %%a in (1,1,254) do ( for /f "Tokens=3 delims=: " %%b in ( '%p%.%%a^|Find /i "TTL="' ) do ( echo Pinging %%b echo %%b>>devices.txt for /f "tokens=2" %%c in ( 'ping -a %%b -l 1 -n 1^|Find /i "pinging"') do ( echo %%c >>hostnames.txt ) ) )

我们首先创建一个以1为增量从1到254的For/L循环,然后在ping命令上运行for循环,该命令在返回行上获取第三个标记并将其放入变量%在这种情况下为%b.然后,我们回显%% b中包含的ping IP地址,并将其写入devices.txt.然后,我们运行另一个ping来提取第二个令牌,它是我们要ping的计算机的主机名,并将其写到hostnames.txt中.

We start by creating a For /L loop that goes from 1 to 254 in increments of 1. Then we run a for loop on a ping command that takes the third token on the return line and puts it into a variable, %%b in this case. We then echo out pinging IP address contained in %%b and write it to devices.txt. Then, we run another ping to extract the second token which is the hostname of the computer we're pinging and write it out to hostnames.txt.

更多推荐

在ping输出中将IP与文本分开?

本文发布于:2023-10-30 00:12:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   文本   ping   IP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!