Board logo

标题: [分享][推荐][原创]按ping返回的时间排序 [打印本页]

作者: scriptor     时间: 2007-10-14 06:05    标题: [分享][推荐][原创]按ping返回的时间排序

想了好久,才想出这么个很丑陋的家伙.
大家轻拍...
CODE:  [Copy to clipboard]
:: Sort by time returned from Ping.
:: Author: Scriptor
:: Contact: Scriptor@GUCAS
:: ..............................................
@echo off
cd.>rez.txt
:: Start Ping to get time!
for /f %%f in (IP.txt) do (
   ping -n 2 %%f |find "time=">>rez.txt
)

:: Sort
setlocal enabledelayedexpansion
cd.>out*.txt
set n=0
for /f "delims==m tokens=5" %%i in (rez.txt) do (
   if %%i gtr !n! set/a n=%%i
)

echo The maxtime is: !n!ms.
echo +++++++++++++++++++++++++++++++++++++++++++++
ping -n 2 127.1>nul
echo 按时间从小到大排序中.....
for /l %%a in (0,1,!n!) do (
  sort /+40 rez.txt|find "=%%ams">>outa.txt
)
echo 排序完毕.
ping -n 2 127.1>nul

作者: scriptor     时间: 2007-10-14 07:03
修改了一下
可以按照TTL来排序
CODE:  [Copy to clipboard]
::
::
@echo off
setlocal enabledelayedexpansion
del out*.txt 2>nul
set/a n=0,t=0

for /f "delims==mL tokens=5,7" %%i in (rez.txt) do (
   if %%i gtr !n! set/a n=%%i
   if %%j gtr !t! set/a t=%%j
)
::pause
echo The maxtime is: !n!ms.
echo The maxTTL time is: !t!.
echo +++++++++++++++++++++++++++++++++++++++++++++
ping -n 1 127.1>nul
echo 按时间从小到大排序中.........
for /l %%a in (0,1,!n!) do (
  type rez.txt|find "=%%ams">>outa.txt
)
echo 排序完毕.
echo 
:l

echo *********************************************
ping -n 1 127.1>nul
echo 按TTL时间从小到大排序中.....
for /l %%b in (1,1,!t!) do (
  type rez.txt|findstr /e TTL=%%b>>outx.txt
)
echo 排序完毕.

作者: lxmxn     时间: 2007-10-14 17:19
To scriptor:

测试了一下,发现如下的问题:

1、“ping -n 2 %%f” 这里ping了两次,无疑对筛选结果产生了影响,而且拖慢了程序的总体运行时间;

2、“cd.>out*.txt” 这句是不是有语法错误了?

3、“sort /+40 rez.txt|find "=%%ams">>outa.txt” 这句中的“sort /+40 rez.txt”貌似可有可无,无明显的效果;

4、“for /l %%a in (0,1,!n!)” 这样的算法未免太慢了?
作者: scriptor     时间: 2007-10-14 23:42
不错哦
你的指出之处,我已经改正了
谢谢!

当时就是急了点,没有优化!
等下上传!
有繁冗的地方,帮帮改一改!
作者: scriptor     时间: 2007-10-14 23:43


  Quote:
Originally posted by lxmxn at 2007-10-14 17:19:
To scriptor:

测试了一下,发现如下的问题:

1、“ping -n 2 %%f” 这里ping了两次,无疑对筛选结果产生了影响,而且拖慢了程序的总体运行时间;

2 ...

贴的第一个代码就不要用了,

另外我能不能删掉第一个代码?会不会影响版面呢?

1: ping两次
是为了防止ping执行的时候,第一次ping返回空,这种情况是有发生过的,
不然的话,我也不会写
ping -n 2 .....
2: 你说的那个sort /+40 已经被替代了---->type ...,看第二段代码;
3: “cd.>out*.txt” 这句  这个已被替代了----> del .... ,看第二段代码;
4: “for /l %%a in (0,1,!n!)” 这样的算法未免太慢了 ---->这个我没有仔细研究,
  你有什么好的想法没?

[ Last edited by scriptor on 2007-10-14 at 11:48 PM ]
作者: scriptor     时间: 2007-10-14 23:55    标题: [修改版]

CODE:  [Copy to clipboard]
::
::修改版
::
@echo off
setlocal enabledelayedexpansion
del out*.txt 2>nul
set/a n=0,t=0,tmin=255
rem tmin是设置TTL排序的起始值,这样可以缩短一些时间;
rem 当然最好是将ttl的所有值放到一个列表中,让循环去检测执行.
rem n的最小值取0是可以适应的;

for /f "delims==mL tokens=5,7" %%i in (rez.txt) do (
   if %%i gtr !n! set/a n=%%i
   if %%j gtr !t! (set/a t=%%j) else (if %%j lss !tmin! set/a tmin=%%j)
)

echo The max_ms time is: !n!ms.
echo The minTTL time is: !tmin!, maxTTL time is: !t!.
echo +++++++++++++++++++++++++++++++++++++++++++++
ping -n 1 127.1>nul
echo 按ms时间从小到大排序中.........
for /l %%a in (0,1,!n!) do (
  type rez.txt|find "=%%ams">>outa.txt
)
echo 排序完毕.
echo 

echo *********************************************
ping -n 1 127.1>nul
echo 按TTL时间从小到大排序中.....
for /l %%b in (!tmin!,1,!t!) do (
  type rez.txt|findstr /e TTL=%%b>>outx.txt
)
echo 排序完毕.

作者: lxmxn     时间: 2007-10-15 07:01


  Quote:
另外我能不能删掉第一个代码?会不会影响版面呢?

可以删掉。
编辑你的贴子,下面有一个“删除此贴”的复选框。
建议直接修改原贴。

  Quote:
1: ping两次
是为了防止ping执行的时候,第一次ping返回空,这种情况是有发生过的,
不然的话,我也不会写
ping -n 2 .....

ping 不通,结果中自然也就没有“time=”这样的字符串,自然也find不到,符合常理。

  Quote:
2: 你说的那个sort /+40 已经被替代了---->type ...,看第二段代码;

我觉得type都是多余的。

  Quote:
3: “cd.>out*.txt” 这句  这个已被替代了----> del .... ,看第二段代码;

没有必要一开始就“del out*.txt”,万一所在目录有一个文件的名字类似这样的呢?
岂不误删???

  Quote:
4: “for /l %%a in (0,1,!n!)” 这样的算法未免太慢了 ---->这个我没有仔细研究,
  你有什么好的想法没?

我觉得“for /L+findstr+sort”更快。
作者: scriptor     时间: 2007-10-15 15:24
好的
我改改
谢谢了
作者: scriptor     时间: 2007-10-15 22:47
我的那个帖子(第一个),怎么提示说没有权力删除啊?
作者: lxmxn     时间: 2007-10-16 01:33
不用删除,直接编辑修改就够了。

实在想删除叫我声,我帮你搞定。
作者: scriptor     时间: 2007-10-16 02:22
你帮我删了吧
还有这个帖子


另外: for/l+sort+find 怎么个做法,我试过一个
但是不能得到按数字的大小排序




欢迎光临 中国DOS联盟论坛 (http://cndos.fam.cx/forum/) Powered by Discuz! 2.5