我目前有连接问题,我想监视某个IP地址并记录这些信息。
我知道我可以使用这个命令(以google.com为例):
ping google.com /t |Foreach{"{0} - {1}" -f (Get-Date),$_} > pingORA.txt在这种情况下,它将每秒钟记录一个ping结果。我希望它每30秒记录一次ping结果。是否有方法将此添加到上面的命令中?
发布于 2019-11-26 12:58:50
使用awk保存每30 ping的结果,丢弃其余的
awk '
BEGIN { srand();t=srand();\
while (1 ) {\
tElapsed=srand()-t;
if(tElapsed%30==0){\
print("write to file from here")\
}\
print ("pinging google");\
system("sleep 1");\
}
}'发布于 2019-11-26 13:53:24
虽然您似乎更想要一个powershell解决方案(因此我也包含了相关的标记),但是您仍然可以在普通的cmd中使用for循环进行相同的操作:
for /f "delims=" %a in ('ping google.com -t') do echo %a >> pingORA.txt & timeout /t 30 >nul 2>&1注意,这是直接来自cmd而不是powershell
发布于 2019-11-26 19:31:18
这可以在PowerShell中使用Test-Connection完成。
$hostName = 'localhost'
$waitTime = 30
$logFile = 'pingORA.txt'
if (Test-Path -Path $logFile) { Remove-Item -Path $logFile }
while (1) {
$p = Test-Connection -ComputerName $hostName -Count 1 -ErrorAction SilentlyContinue
if ($null -ne $p) {
"{0} - {1} - {2}" -f (Get-Date), $hostName, $p.ProtocolAddress |
Out-File -FilePath $logFile -Encoding ascii -Append
} else {
"{0} - {1} - {2}" -f (Get-Date), $hostName, 'offline' |
Out-File -FilePath $logFile -Encoding ascii -Append
}
Start-Sleep -Seconds $waitTime
}https://stackoverflow.com/questions/59049917
复制相似问题