Java設計形式之迭代形式(Iterator形式)引見。本站提示廣大學習愛好者:(Java設計形式之迭代形式(Iterator形式)引見)文章只能為提供參考,不一定能成為您想要的結果。以下是Java設計形式之迭代形式(Iterator形式)引見正文
比來一向在寫一個主動檢測收集內主機類型的劇本。根本功效可以完成斷定主機操作體系類型,假如是域內的主機可以獲得主機的硬件參數和機能參數,並斷定能否存在收集裝備。對一個運維人員來講常常須要盡快熟習一個生疏的收集。所以這個劇本就很便利了,假如有更好的建議迎接斧正感激!
############################################
#Author:Lixiaosong
#Email:[email protected]
#For:檢測/24掩碼收集內主機體系類型並獲得windows主機參數
#Version:1.0
##############################################
Param(
[Parameter(Mandatory=$true)]$Network
)
$Ip=for($i= 1; $i-ile255; $i+= 1){"$Network.$i"}
foreach($Ipaddressin$IP){
#檢測相干端口狀況
$Port3389=3389 | %{ echo ((new-objectNet.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null
$Port22=22 | %{ echo ((new-objectNet.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null
$Port23=23 | %{ echo ((new-objectNet.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null
$Pingtest=Test-connection-ComputerName$IPaddress-quiet
if($Port3389-like"$true"){
#辦事器信息
$HostSN=(GWMI-ComputerName"$Ipaddress"win32_bios).SerialNUmber
$HostFirm=(GWMI-ComputerName"$Ipaddress"win32_bios).Manufacturer
$HostModel=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).Model
#主機信息
$HostName=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).DNSHostName
$DomainName=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).Domain
#辦事器硬件資本信息
$Freemem=(GWMI-ComputerName"$Ipaddress"win32_OperatingSystem).FreePhysicalMemory#空余物理內存
$Totalmem=(GWMI-ComputerName"$Ipaddress"win32_OperatingSystem).TotalVisibleMemorySize#總物理內存
$cpu=((get-counter-ComputerName"$IPaddress"-counter"\processor(_total)\% processor time").CounterSamples|where{$_.InstanceName -eq"_total"}).CookedValue
$DiskRead=" {0:0.0} KB"-f($(((get-counter-ComputerName"$Ipaddress"-counter"\LogicalDisk(_total)\Disk Read Bytes/sec").CounterSamples|where{$_.InstanceName -eq"_total"}).CookedValue) / 1KB)
$DiskWrite="{0:0.0} KB"-f($(((get-counter-ComputerName"$Ipaddress"-counter"\LogicalDisk(_total)\Disk Write Bytes/sec").CounterSamples|where{$_.InstanceName -eq"_total"}).CookedValue) /1KB)
$NetworkSent=" {0:0.0} KB"-f($((Get-Counter-ComputerName"$Ipaddress"-Counter"\Network Interface(*)\Bytes Sent/sec").CounterSamples|%{$_.CookedValue}|sort|select-last1) / 1KB)
$NetworkReceive=" {0:0.0} KB"-f($((Get-Counter-ComputerName"$IPaddress"-Counter"\Network Interface(*)\Bytes Received/sec").CounterSamples|%{$_.CookedValue}|sort|select-last1) / 1KB)
$Havecpu= "{0:0.0} %"-f$cpu
$Permem="{0:0.0} %"-f((($Totalmem-$Freemem)/$Totalmem)*100)
$Disks= GWMI-ComputerName"$IPaddress"win32_logicaldisk|?{$_.drivetype -eq3}
#獲得域內Windows主機參數
Write-host"
=================================================================================================================
時光:$(get-date) WINDOWS辦事器:$HostName.$DomainName IP:$($IPaddress.Padleft(2)) 品牌:$($HostFirm.Padleft(2)) 型號:$($HostModel.Padleft(2)) 序列號:$($HostSN.Padleft(2))
CPU應用率:$($Havecpu.Padleft(8)) 內存應用率:$($Permem.Padleft(13))
磁盤讀/秒:$($DiskRead.Padleft(8)) 磁盤寫/秒:$($DiskWrite.Padleft(13))
收集發送/秒:$($NetworkSent.Padleft(8)) 收集吸收/秒:$($NetworkReceive.Padleft(13))
盤符 盤總空間 余暇空間 應用空間 應用百分比"-ForegroundColorGreen
foreach($Diskin$Disks){
$Size= "{0:0.0} GB"-f($Disk.Size / 1GB )
$FreeSpace= "{0:0.0} GB"-f($Disk.FreeSpace / 1GB)
$Used= ([int64]$Disk.size - [int64]$Disk.freespace)
$SpaceUsed= "{0:0.0} GB"-f($Used/ 1GB)
$Percent="{0:0.0} %"-f($Used* 100 / $Disk.Size)
$n=3
Write-Host" "$Disk.deviceid.PadRight($n)-no-ForegroundColorGreen
$n=10
Write-Host$Size.Padleft($n)-no-ForegroundColorGreen
Write-Host$FreeSpace.Padleft($n)-no-ForegroundColorGreen
Write-Host$SpaceUsed.Padleft($n)-no-ForegroundColorGreen
Write-Host$Percent.Padleft($n)-ForegroundColorGreen
}
}
#斷定linux主機
if($port22-like"$true"){
write-host"
================================================================================================================
辦事器:$IPaddress 開放端口:"22" 能夠是一台是"linux"主機"-ForegroundColorYellow
}
#斷定收集裝備
if($port23-like"$true"){
Write-host"
================================================================================================================
辦事器:$Ipaddress 開放端口:"23" 能夠是一台"收集"裝備"-ForegroundColorCyan
}
#主機不存在
if($Pingtest-like"$False"){
Write-host"
================================================================================================================
辦事器:$Ipaddress 此主機不存在"-ForegroundColorRed
}
}
應用辦法舉例:
1 將劇本保留至c:\
2 運轉powershell 履行PS C:\> .\test.ps1 10.7.2 #只需輸出收集的前三位
PS C:\> .\test.ps1 10.7.2