PowerShell实战1:Ping_Test
生活随笔
收集整理的這篇文章主要介紹了
PowerShell实战1:Ping_Test
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
功能:批量測(cè)試遠(yuǎn)程主機(jī)的Ping值,以及根據(jù)TTL值來(lái)判斷是否為Windows主機(jī)。
使用:在C:\IP.txt中加入需要測(cè)試的主機(jī)IP或域名,一行一個(gè)。例如: www.google.com
www.baidu.com
www.cha123.com
www.yahoo.com
www.msn.com 源碼: function Ping_Test {
??? PROCESS {
????? $ping = $false
????? $results = Get-WmiObject -query `
????? "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
????? $RT = $results.ResponseTime
????? $TTL = $results.ResponseTimeToLive
????? foreach ($result in $results) {
??????? if ($results.StatusCode -eq 0) {
??????????? if ($TTL -ge 98 -and $TTL -le 128)
??????????????? {Write-Host "`n$_ Response Time=$RT ms, TTL=$TTL,It is a Windows host." -ForegroundColor Green }
????????????? else
????????????????? {Write-Host "`n$_ Response Time=$RT ms, TTL=$TTL, It is NOT a Windows host." -ForegroundColor Blue}
????????? }
??????? else {
????????? Write-Host "`n$_ Ping failed!" -ForegroundColor Red
??????? }
??????? }
??? }
? }
cls
Get-Content c:\IP.txt | Ping_Test 結(jié)果:
使用:在C:\IP.txt中加入需要測(cè)試的主機(jī)IP或域名,一行一個(gè)。例如: www.google.com
www.baidu.com
www.cha123.com
www.yahoo.com
www.msn.com 源碼: function Ping_Test {
??? PROCESS {
????? $ping = $false
????? $results = Get-WmiObject -query `
????? "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
????? $RT = $results.ResponseTime
????? $TTL = $results.ResponseTimeToLive
????? foreach ($result in $results) {
??????? if ($results.StatusCode -eq 0) {
??????????? if ($TTL -ge 98 -and $TTL -le 128)
??????????????? {Write-Host "`n$_ Response Time=$RT ms, TTL=$TTL,It is a Windows host." -ForegroundColor Green }
????????????? else
????????????????? {Write-Host "`n$_ Response Time=$RT ms, TTL=$TTL, It is NOT a Windows host." -ForegroundColor Blue}
????????? }
??????? else {
????????? Write-Host "`n$_ Ping failed!" -ForegroundColor Red
??????? }
??????? }
??? }
? }
cls
Get-Content c:\IP.txt | Ping_Test 結(jié)果:
轉(zhuǎn)載于:https://blog.51cto.com/jackiechen/202580
總結(jié)
以上是生活随笔為你收集整理的PowerShell实战1:Ping_Test的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到抓了好几条大鱼
- 下一篇: Session莫名丢失的原因及解决办法[