背景
项目中,无线路由器连接着一个agv,agv是移动着的,还连接着其他的设备,需要监控一下这些设备的网络情况,因为之前出现互相调用接口 超时的情况
windows
编写一个powershell脚本,将ping的日志写入文件中
ping.exe -t 192.168.1.165 | Foreach{"{0} - {1}" -f (Get-Date),$_} > 192.168.1.165.txt
输出日志格式为:2023/6/16 9:52:14 - 来自 192.168.1.165 的回复: 字节=32 时间=1ms TTL=64
可以满足需求
下面还有几个其他的版本
版本1
#Create a file in the required path and update in the below command line
$Output ="Output.csv"
#The output field of the computer will blank if the user is already exist in the group
Add-Content -Path $Output -Value"ComputerName,Availability,Status"
$status = $null
$availability = $null
#Save the CSV (Comma seperated) file with the server host name and the username to be added
$result = Import-Csv Computer.csv | ForEach-Object {
$Computer=$_.Computer
$User=$_.user
if (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
Write-Verbose"$Computer : Online"
$availability="Oniline"
try {
$GroupObj=[ADSI]"WinNT://$Computer/Administrators,group"
$GroupObj.PSBase.Invoke("Add",([ADSI]"WinNT://jdom.edu/$User").Path)
$status="Success"
#Update the status in the output file
[PSCustomObject]@{
Computer = $Computer
Availability = $availability
Status = $status
Date = Get-Date
}
} catch {
Write-Verbose"Failed"
}
} else {
Write-Warning"$Computer : Offline"
$availability ="Offline"
$status ="failed"
#Update the status in the output file
[PSCustomObject]@{
Computer = $Computer
Availability = $availability
Status = $status
Date = Get-Date
}
}
}
$result | Export-Csv -Path $Output -NoTypeInformation
版本2
$testOutput= @()
$testnames = Get-Content "Computer.txt"
foreach ($name in $testnames){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
$testOutput+= "$name,Up,$Get-Date"
Write-Host "$Name,Up,$Get-Date"
}
else{
$testOutput+= "$name,Down,$Get-Date"
Write-Host "$Name,Down,$Get-Date"
}
}
$testOutput | Out-File "Output.txt"
版本3
$testOutput= @()
$testnames = Get-Content "Computer.txt"
foreach ($name in $testnames){
$currentTime = Get-Date -format "yyyy-MM-dd HH:mm:ss fff"
$testOutput+= "$currentTime : ping -t $name "
}
$testOutput | Out-File "Output.txt"
Linux
支持同时监测多个ip,将ping的结果格式化输出到log文件中
#!/bin/bash
# 检测IP地址列表
ip_list=( "192.168.3.21" "192.168.3.31" "192.168.3.4" )
while true
do
# 获取当前日期和时间戳
timestamp=$(date +%s.%N)
date_str=$(date +%Y-%m-%d)
# 遍历IP地址列表并执行ping测试
for ip in "${ip_list[@]}"
do
# 获取日志文件路径
log_dir="/home/pingLog/$ip"
log_file="$log_dir/$date_str.log"
# 创建日志文件目录
mkdir -p $log_dir
# 执行ping测试并计时
ping_res=$(timeout 1 ping -i 0.2 -c 4 $ip)
duration=$(echo "$(date +%s.%N) - $timestamp" | bc)
# 解析ping测试结果
loss_rate=$(echo $ping_res | grep -oP '\d+(?=% packet loss)')
min_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $6}' FS='[ /]')
avg_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $7}' FS='[ /]')
max_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $8}' FS='[ /]')
mdev_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $9}' FS='[ /]')
#rtt min/avg/max/mdev = 0.556/0.611/0.669/0.057 ms
# 格式化输出测试结果到日志文件
log_time=$(date "+%H:%M:%S")
log_line="$log_time "
if [ $loss_rate -eq 0 ]; then
log_line+="online"
else
log_line+="offline"
fi
log_line+=" $loss_rate $min_time $avg_time $max_time $mdev_time"
#log_line+="[loss rate:$loss_rate,min time:$min_time ms,avg time:$avg_time ms,max time:$max_time ms,mdev time:$mdev_time ms]"
#log_line+="[loss rate:$loss_rate,min time:$min_time ms,avg time:$avg_time ms,max time:$max_time ms,mdev time:$mdev_time ms]"$'\n'"$ping_res"
echo "$log_line" >> "$log_file"
# 计算并暂停余下时间
duration=$(echo "scale=1; 1.0 - $duration" | bc)
if (( $(echo "$duration > 0" | bc -l) )); then
sleep $duration
fi
done
done
其他版本
版本1
#!/bin/bash
# 检测IP地址列表
ip_list=( "192.168.1.165" "192.168.1.175" "192.168.123.123" )
while true
do
# 获取当前日期和时间戳
timestamp=$(date +%s.%N)
date_str=$(date +%Y-%m-%d)
# 遍历IP地址列表并执行ping测试
for ip in "${ip_list[@]}"
do
# 获取日志文件路径
log_dir="/root/pingLog/$ip"
log_file="$log_dir/$date_str.log"
# 创建日志文件目录
mkdir -p $log_dir
# 执行ping测试并计时
ping_res=$(timeout 1 ping -i 0.2 -c 1 $ip)
duration=$(echo "$(date +%s.%N) - $timestamp" | bc)
# 解析ping测试结果
loss_rate=$(echo $ping_res | grep -oP '\d+(?=% packet loss)')
min_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $6}' FS='[ /]')
avg_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $7}' FS='[ /]')
max_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $8}' FS='[ /]')
mdev_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $9}' FS='[ /]')
#rtt min/avg/max/mdev = 0.556/0.611/0.669/0.057 ms
# 格式化输出测试结果到日志文件
log_time=$(date "+%H:%M:%S")
log_line="$log_time "
if [ $loss_rate -eq 0 ]; then
log_line+="online"
else
log_line+="offline"
fi
log_line+=" $loss_rate $min_time $avg_time $max_time $mdev_time"
#log_line+="[loss rate:$loss_rate,min time:$min_time ms,avg time:$avg_time ms,max time:$max_time ms,mdev time:$mdev_time ms]"
#log_line+="[loss rate:$loss_rate,min time:$min_time ms,avg time:$avg_time ms,max time:$max_time ms,mdev time:$mdev_time ms]"$'\n'"$ping_res"
echo "$log_line" >> "$log_file"
# 计算并暂停余下时间
duration=$(echo "scale=1; 1.0 - $duration" | bc)
if (( $(echo "$duration > 0" | bc -l) )); then
sleep $duration
fi
done
done
版本2
#!/bin/bash
# 检测IP地址列表
ip_list=( "192.168.1.165" "192.168.1.175" "192.168.123.123" )
while true
do
# 获取当前日期
date_str=$(date "+%Y-%m-%d")
# 遍历IP地址列表并执行ping测试
for ip in "${ip_list[@]}"
do
# 获取日志文件路径
log_dir="/root/pingLog/$ip"
log_file="$log_dir/$date_str.log"
# 创建日志文件目录
mkdir -p $log_dir
# 执行ping测试
ping_res=$(ping -i 1 -c 4 $ip)
log_time=$(date "+%Y-%m-%d %H:%M:%S")
# 解析ping测试结果
loss_rate=$(echo $ping_res | grep -oP '\d+(?=% packet loss)')
min_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $6}' FS='[ /]')
avg_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $7}' FS='[ /]')
max_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $8}' FS='[ /]')
mdev_time=$(echo $ping_res | grep -oP 'min/avg/max/mdev = \d+\.\d+/\d+\.\d+/\d+\.\d+/\d+\.\d+' | awk '{print $9}' FS='[ /]')
# 格式化输出测试结果到日志文件
log_line="$log_time "
if [ $loss_rate -eq 0 ]; then
log_line+="online"
else
log_line+="offline"
fi
log_line+=" $loss_rate $min_time $avg_time $max_time $mdev_time"
#log_line+="[loss rate:$loss_rate,min time:$min_time ms,avg time:$avg_time ms,max time:$max_time ms,mdev time:$mdev_time ms]"
#log_line+="[loss rate:$loss_rate,min time:$min_time ms,avg time:$avg_time ms,max time:$max_time ms,mdev time:$mdev_time ms]"$'\n'"$ping_res"
echo "$log_line" >> $log_file
#echo "$log_time $ping_res" >> $log_file
done
# 每次执行完毕后等待 1 秒再继续
sleep 1
done
版本3
#!/bin/sh
ping -i 1 192.168.1.165 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }' >> /root/192.168.1.165.log
ping -i 1 192.168.1.175 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }' >> /root/192.168.1.175.log
echo "192.168.1.165 192.168.1.175 192.168.1.185" | xargs -n 1 -P 0 ping -i 1 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }' >> /root/ping.log
总结
网络波动可能会有很多情况导致
比如 网卡设备的规格,多个wifi信号影响,金属 等
这次主要是更换了agv里的无线网卡,换成了工业级的无线客户端
可以从下面几个方向入手查看原因:
- 进入路由器管理页面,看看内存和流量的监控情况
- 查看现场是否有其他wifi信号影响,可以尝试更改频段和信道
还可以使用 tcpdump工具、Wireshark 进行网络监测