![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-09 22:30 |
47,811 topics / 349,897 posts / today 2 new / 48,255 members |
| DOS批处理 & 脚本技术(批处理室) » Help: Batch script to implement LAN server monitoring using the ping command |
| Printable Version 2,762 / 16 |
| Floor1 fancyli622 | Posted 2008-09-21 13:15 |
| 初级用户 Posts 10 Credits 22 | |
|
I've just started learning batch processing for a short time, and I'm a bit stuck with the boss's requirement. I'm asking you all for help:
Requirement: 1) There are 40 servers in the LAN. It's required to use ping -l 10000 to connect with each other, that is, 40*40 pairs; 2) Take out the "Average" value from the obtained ping results. For each machine, there should be 40 "Average" values; 3) Take the average of the 40 obtained "Average" values for each machine to get one "Average" value; 4) There are 40 "Average" values obtained from mutual pings for 40 machines; 5) Generate a dot chart from the obtained 40 results, and achieve the purpose of network monitoring by viewing the change curve. (My idea is to run a batch processing on each machine to collect results to one machine, then import the 40 results into an Excel sheet, and then generate a dot chart...) ![]() Thanks... |
|
| Floor2 fancyli622 | Posted 2008-09-21 13:15 |
| 初级用户 Posts 10 Credits 22 | |
|
Or everyone has any better ideas, welcome to put forward :-)
|
|
| Floor3 HAT | Posted 2008-09-21 13:40 |
| 版主 Posts 5,017 Credits 9,023 | |
|
You can go to this post to see how to get the "Average" value of the ping result
http://www.cn-dos.net/forum/viewthread.php?tid=40395 |
|
| Floor4 fancyli622 | Posted 2008-09-21 14:09 |
| 初级用户 Posts 10 Credits 22 | |
|
To sum the 40 "Average" times and find the average divided by 40, you can use a batch script to handle the calculations. Here's an example of how you can modify the existing script to achieve this:
```batch @echo off setlocal enabledelayedexpansion set sum=0 set count=0 for /f %%i in (c:\build_pcs.txt) do ( ping -l 1000 %%i | find "Average =" >>e:\ping_Average_time.txt ) for /f "tokens=4 delims==" %%i in (e:\ping_Average_time.txt) do ( set /a sum=!sum! + %%i set /a count+=1 ) set /a average=!sum! / !count! echo The average of the 40 Average times is: !average! ms endlocal ``` This script first initializes a sum variable and a count variable. It then iterates through the lines in `e:\ping_Average_time.txt`, extracts the numerical part of the "Average" time, adds it to the sum, and increments the count. Finally, it calculates the average by dividing the sum by the count and displays the result. |
|
| Floor5 HAT | Posted 2008-09-21 14:42 |
| 版主 Posts 5,017 Credits 9,023 | |
|
```@echo off
set sum=0 for /f "delims=ms" %%a in (ping_Average_time.txt) do ( set /a sum+=%%a ) echo Sum of the average values: %sum% >"%temp%\MyDel.vbs" echo wscript.echo %sum%/40 for /f %%a in ('cscript /nologo "%temp%\MyDel.vbs"') do set div1=%%a echo Result of average divided by 40: %div1% set /a div2=sum/40 echo Result of average integer division by 40: %div2% ``` |
|
| Floor6 fancyli622 | Posted 2008-09-21 16:00 |
| 初级用户 Posts 10 Credits 22 | |
|
Hehe, thank you for HAT's help. Also, can you give some comments so that I, a beginner, can understand? Especially the following two sentences?
>"%temp%\MyDel.vbs" echo wscript.echo %sum%/40 for /f %%a in ('cscript /nologo "%temp%\MyDel.vbs"') do set div1=%%a |
|
| Floor7 HAT | Posted 2008-09-21 16:19 |
| 版主 Posts 5,017 Credits 9,023 | |
>"%temp%\MyDel.vbs" echo wscript.echo %sum%/40 Because in batch processing, / means integer division, so use the above method to dynamically generate a vbs file to calculate the division. for /f %%a in ('cscript /nologo "%temp%\MyDel.vbs"') do set div1=%%a Call the just-generated vbs in batch processing and assign the calculation result to the variable div1 |
|
| Floor8 fancyli622 | Posted 2008-09-21 22:10 |
| 初级用户 Posts 10 Credits 22 | |
|
The first 4 requirements are okay, but how to automatically generate a dot plot?
My idea is: Can the results be imported into the already made Excel template, and worksheet 2 directly generates a dot plot from the data obtained in worksheet 1? Now a TXT file is obtained as the following example (but if an Excel file is directly obtained, it will be placed in the same cell?): ---------------------------------------------------------- xty1UEA 2.186046512 xtyP61C 2.162790698 xtyP82C 2.023255814 xty1UDA 1.837209302 xtyPQ6C 1.465116279 xtyP84C 2.348837209 xtyPS7C 1.790697674 xtyPQ3C 1.372093023 xtyPQ2C 1.651162791 xtyPS9C 2.790697674 xtyPQ4C 1.395348837 xtyPQ5C 2.093023256 xtyPT0C 3.11627907 xtyPT1C 2.11627907 xtyPQ7C 1.627906977 xtyPT2C 1.88372093 xtyPR1C 2.418604651 xtyPT3C 1.976744186 xtyPR2C 2.302325581 xtyPT4C 1.906976744 xtyPT5C 2.395348837 xtyPR3C 2.302325581 xtyPR7C 3 xtyPT6C 1.627906977 xtyPR4C 2.906976744 xtyPT7C 2.069767442 xtyPT8C 2.093023256 xtyPR5C 2.441860465 xtyPR6C 2.372093023 xtyPT9C 2.023255814 xtyPU0C 2.279069767 xtyPS3C 1.953488372 xtyPS4C 2.302325581 -------------------------------------------------------------- |
|
| Floor9 fancyli622 | Posted 2008-09-21 22:11 |
| 初级用户 Posts 10 Credits 22 | |
|
Forgot, there's also an estimated network issue. Originally, there should be 40 machines, but the collected results are a few less? I took a look, and the scripts for these few machines can run, but the results didn't get transmitted to the unified place? What's going on here?
|
|
| Floor10 HAT | Posted 2008-09-21 22:29 |
| 版主 Posts 5,017 Credits 9,023 | |
|
Replace the spaces in your data with tabs. Open Excel -> Data -> Import External Data ->......
|
|
| Floor11 HAT | Posted 2008-09-21 22:31 |
| 版主 Posts 5,017 Credits 9,023 | |
|
What is the code in your actual application? How do you transfer data to a unified place?
|
|
| Floor12 fancyli622 | Posted 2008-09-21 22:46 |
| 初级用户 Posts 10 Credits 22 | |
|
Aren't there 40 machines? I set up a scheduled task on each machine to run the following bat file:
----------------------------------------- @echo off if exist e:\ping_time.txt del /q e:\ping_time.txt if exist e:\ping_Average_time.txt del /q e:\ping_Average_time.txt if exist e:\ping_Average_time.xls del /q e:\ping_Average_time.xls for /f %%i in (e:\build_pcs.txt) do ping -l 1000 %%i | find "Average =" >>e:\ping_time.txt for /f "tokens=4 delims==" %%i in (e:\ping_time.txt) do @echo %%i >>e:\ping_Average_time.txt set sum=0 for /f "delims=ms" %%a in (e:\ping_Average_time.txt) do ( set /a sum+=%%a ) echo 平均值的和:%sum% REM*****Since / in batch processing means integer division, dynamically generate a vbs file to calculate division.***** >"%temp%\MyDel.vbs" echo wscript.echo %sum%/43 REM*****Call the just-generated vbs in batch processing and assign the calculation result to variable div1***** for /f %%a in ('cscript /nologo "%temp%\MyDel.vbs"') do set div1=%%a echo %Computername% %div1% >>\\xtyPR3c\ping_Average_time.txt -------------------------------------------------------------------- Just transfer the data to a unified place :-) Of course, it can finally be exported to an xls file, but after exporting to xls, it won't be divided into two columns, but one column, which is not easy to count? I just want to use an automatic clicking method to export to an xls file and automatically divide into two columns. It's best to export to worksheet 1 of a pre-made template, so that the made template can automatically generate a curve chart in worksheet 2 :-) |
|
| Floor13 fancyli622 | Posted 2008-09-21 22:48 |
| 初级用户 Posts 10 Credits 22 | |
|
Finally, also want to reverse this generated xls file and automatically send an email to the administrator?
|
|
| Floor14 HAT | Posted 2008-09-21 23:10 |
| 版主 Posts 5,017 Credits 9,023 | |
|
First, modify the last line of the code on the problematic machines. Instead of redirecting the file to xtyPR3c, redirect it to the local machine for testing. As for splitting into two columns in xls, try replacing the space between %Computername% and %div1% with a tab character.
|
|
| Floor15 HAT | Posted 2008-09-21 23:13 |
| 版主 Posts 5,017 Credits 9,023 | |
|
You can use VBS to send emails. Just Google it; similar code is everywhere.
If you need to send attachments, you can refer to this thread: http://www.cn-dos.net/forum/viewthread.php?tid=29873 [ Last edited by HAT on 2008-11-6 at 00:56 ] |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |