China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-09 21:36
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Help: Batch script to implement LAN server monitoring using the ping command View 2,757 Replies 16
Original Poster Posted 2008-09-21 13:15 ·  中国 广东 深圳 天威视讯
初级用户
Credits 22
Posts 10
Joined 2008-09-21 12:30
17-year member
UID 126203
Gender Female
Status Offline
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...
Floor 2 Posted 2008-09-21 13:15 ·  中国 广东 深圳 天威视讯
初级用户
Credits 22
Posts 10
Joined 2008-09-21 12:30
17-year member
UID 126203
Gender Female
Status Offline
Or everyone has any better ideas, welcome to put forward :-)
Floor 3 Posted 2008-09-21 13:40 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
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
Floor 4 Posted 2008-09-21 14:09 ·  中国 广东 深圳 天威视讯
初级用户
Credits 22
Posts 10
Joined 2008-09-21 12:30
17-year member
UID 126203
Gender Female
Status Offline
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.
Floor 5 Posted 2008-09-21 14:42 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
```@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%
```
Floor 6 Posted 2008-09-21 16:00 ·  中国 广东 深圳 天威视讯
初级用户
Credits 22
Posts 10
Joined 2008-09-21 12:30
17-year member
UID 126203
Gender Female
Status Offline
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
Floor 7 Posted 2008-09-21 16:19 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
>"%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
Floor 8 Posted 2008-09-21 22:10 ·  中国 广东 深圳 天威视讯
初级用户
Credits 22
Posts 10
Joined 2008-09-21 12:30
17-year member
UID 126203
Gender Female
Status Offline
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
--------------------------------------------------------------
Floor 9 Posted 2008-09-21 22:11 ·  中国 广东 深圳 天威视讯
初级用户
Credits 22
Posts 10
Joined 2008-09-21 12:30
17-year member
UID 126203
Gender Female
Status Offline
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?
Floor 10 Posted 2008-09-21 22:29 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Replace the spaces in your data with tabs. Open Excel -> Data -> Import External Data ->......
Floor 11 Posted 2008-09-21 22:31 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
What is the code in your actual application? How do you transfer data to a unified place?
Floor 12 Posted 2008-09-21 22:46 ·  中国 广东 深圳 天威视讯
初级用户
Credits 22
Posts 10
Joined 2008-09-21 12:30
17-year member
UID 126203
Gender Female
Status Offline
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 :-)
Floor 13 Posted 2008-09-21 22:48 ·  中国 广东 深圳 天威视讯
初级用户
Credits 22
Posts 10
Joined 2008-09-21 12:30
17-year member
UID 126203
Gender Female
Status Offline
Finally, also want to reverse this generated xls file and automatically send an email to the administrator?
Floor 14 Posted 2008-09-21 23:10 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
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.
Floor 15 Posted 2008-09-21 23:13 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
You can use VBS to send emails. Just Google it; similar code is everywhere.
Set cdo = CreateObject("CDO.Message")
strCfg = "http://schemas.microsoft.com/cdo/configuration/"
With cdo
.Sender = "Sender's email address"
.From = "Sender's email address"
.To = "Recipient's email address"
.Fields("urn:schemas:mailheader:X-Priority") = 1
.Fields.Update
.Subject = "Email subject"
.TextBody = "Email content"
.Configuration(strCfg & "SendUsing") = 2
.Configuration(strCfg & "smtpserver") = "SMTP server address for sending emails"
.Configuration.Fields.Update
.Send
End With


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 ]
Forum Jump: