Board logo

标题: [文本处理] 求助批处理如何提取ping平均值? [打印本页]

作者: laotou1000    时间: 2009-10-9 11:14     标题: 求助批处理如何提取ping平均值?

我编写了一个提取拼平均值的批处理代码,如下:
  1. @echo off
  2. for /l %%p in (130,1,150) do @ping 172.16.3.%%p -n 1 -w 10 | find " " /i >>旧.txt
  3. (for /f "tokens=1,2,6 delims== " %%i in ('findstr /i "Pinging Average" 旧.txt') do if /i "%%i" == "Pinging" (set /p =%%j:<nul) else set /p =Average=%%k<nul&echo.)>>新.txt
  4. pause
复制代码
运行结果如下:
  1. 旧.txt
  2. Pinging 172.16.3.130 with 32 bytes of data:
  3. Reply from 172.16.3.130: bytes=32 time=58ms TTL=64
  4. Ping statistics for 172.16.3.130:
  5.     Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
  6. Approximate round trip times in milli-seconds:
  7.     Minimum = 58ms, Maximum = 58ms, Average = 58ms
  8. Pinging 172.16.3.131 with 32 bytes of data:
  9. Request timed out.
  10. Ping statistics for 172.16.3.131:
  11.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  12. Pinging 172.16.3.132 with 32 bytes of data:
  13. Reply from 172.16.3.132: bytes=32 time<1ms TTL=128
  14. Ping statistics for 172.16.3.132:
  15.     Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
  16. Approximate round trip times in milli-seconds:
  17.     Minimum = 0ms, Maximum = 0ms, Average = 0ms
  18. Pinging 172.16.3.133 with 32 bytes of data:
  19. Request timed out.
  20. Ping statistics for 172.16.3.133:
  21.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  22. Pinging 172.16.3.134 with 32 bytes of data:
  23. Request timed out.
  24. Ping statistics for 172.16.3.134:
  25.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  26. Pinging 172.16.3.135 with 32 bytes of data:
  27. Request timed out.
  28. Ping statistics for 172.16.3.135:
  29.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  30. Pinging 172.16.3.136 with 32 bytes of data:
  31. Request timed out.
  32. Ping statistics for 172.16.3.136:
  33.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  34. Pinging 172.16.3.137 with 32 bytes of data:
  35. Request timed out.
  36. Ping statistics for 172.16.3.137:
  37.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  38. Pinging 172.16.3.138 with 32 bytes of data:
  39. Request timed out.
  40. Ping statistics for 172.16.3.138:
  41.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  42. Pinging 172.16.3.139 with 32 bytes of data:
  43. Request timed out.
  44. Ping statistics for 172.16.3.139:
  45.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  46. Pinging 172.16.3.140 with 32 bytes of data:
  47. Request timed out.
  48. Ping statistics for 172.16.3.140:
  49.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  50. Pinging 172.16.3.141 with 32 bytes of data:
  51. Request timed out.
  52. Ping statistics for 172.16.3.141:
  53.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  54. Pinging 172.16.3.142 with 32 bytes of data:
  55. Request timed out.
  56. Ping statistics for 172.16.3.142:
  57.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  58. Pinging 172.16.3.143 with 32 bytes of data:
  59. Reply from 172.16.3.143: bytes=32 time=1ms TTL=128
  60. Ping statistics for 172.16.3.143:
  61.     Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
  62. Approximate round trip times in milli-seconds:
  63.     Minimum = 1ms, Maximum = 1ms, Average = 1ms
  64. Pinging 172.16.3.144 with 32 bytes of data:
  65. Request timed out.
  66. Ping statistics for 172.16.3.144:
  67.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  68. Pinging 172.16.3.145 with 32 bytes of data:
  69. Request timed out.
  70. Ping statistics for 172.16.3.145:
  71.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  72. Pinging 172.16.3.146 with 32 bytes of data:
  73. Request timed out.
  74. Ping statistics for 172.16.3.146:
  75.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  76. Pinging 172.16.3.147 with 32 bytes of data:
  77. Request timed out.
  78. Ping statistics for 172.16.3.147:
  79.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  80. Pinging 172.16.3.148 with 32 bytes of data:
  81. Request timed out.
  82. Ping statistics for 172.16.3.148:
  83.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  84. Pinging 172.16.3.149 with 32 bytes of data:
  85. Request timed out.
  86. Ping statistics for 172.16.3.149:
  87.     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
  88. Pinging 172.16.3.150 with 32 bytes of data:
  89. Reply from 172.16.3.150: bytes=32 time=1ms TTL=128
  90. Ping statistics for 172.16.3.150:
  91.     Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
  92. Approximate round trip times in milli-seconds:
  93.     Minimum = 1ms, Maximum = 1ms, Average = 1ms
复制代码
  1. 新.txt
  2. 172.16.3.130:Average=58ms
  3. 172.16.3.131:172.16.3.132:Average=0ms
  4. 172.16.3.133:172.16.3.134:172.16.3.135:172.16.3.136:172.16.3.137:172.16.3.138:172.16.3.139:172.16.3.140:172.16.3.141:172.16.3.142:172.16.3.143:Average=1ms
  5. 172.16.3.144:172.16.3.145:172.16.3.146:172.16.3.147:172.16.3.148:172.16.3.149:172.16.3.150:Average=1ms
复制代码
现在我想要将 新.txt 中的显示内容修改为
  1. 172.16.3.130:Average=58ms
  2. 172.16.3.131:
  3. 172.16.3.132:Average=0ms
  4. 172.16.3.133:
  5. 172.16.3.134:
  6. 172.16.3.135:
  7. 172.16.3.136:
  8. 172.16.3.137:
  9. 172.16.3.138:
  10. 172.16.3.139:
  11. 172.16.3.140:
  12. 172.16.3.141:
  13. 172.16.3.142:
  14. 172.16.3.143:Average=1ms
  15. 172.16.3.144:
  16. 172.16.3.145:
  17. 172.16.3.146:
  18. 172.16.3.147:
  19. 172.16.3.148:
  20. 172.16.3.149:
  21. 172.16.3.150:Average=1ms
复制代码
即每个IP后面跟一个平均值,而且一个IP一行,我应该如何修改代码?请各位前辈指教。

[ 本帖最后由 laotou1000 于 2009-10-9 11:16 编辑 ]
作者: hlzj88    时间: 2009-10-9 23:22

看这个贴挂了12小时,没人发言,我就说一下我的理解,因为写不出
set /p =%%j:<nul  可能是这句导致后面新.txt数据连续
Average = 1ms  如果要取这句,可以 skip=n 掉前面的几行 用delims或findstr来截取到Average = 1ms(也可以截取TTL) 再set a后得到 1(先去掉Average = )(再倒过来截去掉ms)  ,再set b先等于0,令set b=(set a)+b(这里的b是多个a相加),再除以(150-130)就是你要的平均值,貌似不用写入文本,关键在截取到数字

不会说,希望我的思路你能理解。
作者: laotou1000    时间: 2009-10-10 09:21     标题: 回复 2楼 的帖子

感谢hlzj88的热心帮助!!!
不过在“再set b先等于0,令set b=(set a)+b(这里的b是多个a相加),再除以(150-130)就是你要的平均值”中,为什么要让多个a相加,再除以(150-130)呢?
作者: hlzj88    时间: 2009-10-10 20:32

a 是每次ping得到的数,b是每次a的值相加的和,(150-130)是你ping的总次数。

刚在线翻译了一下。Average为平均,哈哈,英语不行,就是不行。各位见笑了。

[ 本帖最后由 hlzj88 于 2009-10-10 20:38 编辑 ]
作者: laotou1000    时间: 2009-10-12 08:53     标题: 回复 4楼 的帖子

您好像弄错了吧?我要的不是您说的这个,我是想要从ping的结果中提取其中的average。
作者: duanlihong    时间: 2009-11-23 14:35     标题: 请参考

  1. @echo off&setlocal enabledelayedexpansion
  2. echo *****************************监测启动****************************************
  3. cd c:\
  4. for /f "tokens=1* delims=:" %%a in (a.txt) do (
  5.     for /f  "tokens=3 delims=," %%i in ('ping -l 1000 -n 2 %%a ^|findstr Average') do echo %%a %%i>>ping.txt
  6.     )
  7. pause
复制代码





欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2