最近一直在寫一個(gè)自動(dòng)檢測(cè)網(wǎng)絡(luò)內(nèi)主機(jī)類型的腳本?;竟δ芸梢詫?shí)現(xiàn)判斷主機(jī)操作系統(tǒng)類型,如果是域內(nèi)的主機(jī)可以獲取主機(jī)的硬件參數(shù)和性能參數(shù),并判斷是否存在網(wǎng)絡(luò)設(shè)備。對(duì)一個(gè)運(yùn)維人員來說往往需要盡快熟悉一個(gè)陌生的網(wǎng)絡(luò)。所以這個(gè)腳本就很方便了,如果有更好的建議歡迎指正感謝!
復(fù)制代碼 代碼如下:
############################################
#Author:Lixiaosong
#Email:lixiaosong8706@gmail.com
#For:檢測(cè)/24掩碼網(wǎng)絡(luò)內(nèi)主機(jī)系統(tǒng)類型并獲取windows主機(jī)參數(shù)
#Version:1.0
##############################################
Param(
[Parameter(Mandatory=$true)]$Network
)
$Ip=for($i= 1; $i-ile255; $i+= 1){"$Network.$i"}
foreach($Ipaddressin$IP){
#檢測(cè)相關(guān)端口狀態(tài)
$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"){
#服務(wù)器信息
$HostSN=(GWMI-ComputerName"$Ipaddress"win32_bios).SerialNUmber
$HostFirm=(GWMI-ComputerName"$Ipaddress"win32_bios).Manufacturer
$HostModel=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).Model
#主機(jī)信息
$HostName=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).DNSHostName
$DomainName=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).Domain
#服務(wù)器硬件資源信息
$Freemem=(GWMI-ComputerName"$Ipaddress"win32_OperatingSystem).FreePhysicalMemory#空余物理內(nèi)存
$Totalmem=(GWMI-ComputerName"$Ipaddress"win32_OperatingSystem).TotalVisibleMemorySize#總物理內(nèi)存
$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}
#獲取域內(nèi)Windows主機(jī)參數(shù)
Write-host"
=================================================================================================================
時(shí)間:$(get-date) WINDOWS服務(wù)器:$HostName.$DomainName IP:$($IPaddress.Padleft(2)) 品牌:$($HostFirm.Padleft(2)) 型號(hào):$($HostModel.Padleft(2)) 序列號(hào):$($HostSN.Padleft(2))
CPU使用率:$($Havecpu.Padleft(8)) 內(nèi)存使用率:$($Permem.Padleft(13))
磁盤讀/秒:$($DiskRead.Padleft(8)) 磁盤寫/秒:$($DiskWrite.Padleft(13))
網(wǎng)絡(luò)發(fā)送/秒:$($NetworkSent.Padleft(8)) 網(wǎng)絡(luò)接收/秒:$($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
}
}
#判斷l(xiāng)inux主機(jī)
if($port22-like"$true"){
write-host"
================================================================================================================
服務(wù)器:$IPaddress 開放端口:"22" 可能是一臺(tái)是"linux"主機(jī)"-ForegroundColorYellow
}
#判斷網(wǎng)絡(luò)設(shè)備
if($port23-like"$true"){
Write-host"
================================================================================================================
服務(wù)器:$Ipaddress 開放端口:"23" 可能是一臺(tái)"網(wǎng)絡(luò)"設(shè)備"-ForegroundColorCyan
}
#主機(jī)不存在
if($Pingtest-like"$False"){
Write-host"
================================================================================================================
服務(wù)器:$Ipaddress 此主機(jī)不存在"-ForegroundColorRed
}
}
使用方法舉例:
1 將腳本保存至c:\
2 運(yùn)行powershell 執(zhí)行PS C:\&; .\test.ps1 10.7.2 #只需輸入網(wǎng)絡(luò)的前三位
復(fù)制代碼 代碼如下:
PS C:\&; .\test.ps1 10.7.2
您可能感興趣的文章:- 自動(dòng)化下載并檢測(cè)ftp文件備份的shell腳本
- 用來檢測(cè)輸入的選項(xiàng)$1是否在PATH中的shell腳本
- shell腳本實(shí)現(xiàn)實(shí)時(shí)檢測(cè)文件變更
- Shell腳本實(shí)現(xiàn)檢測(cè)Cygwin最快的鏡像站點(diǎn)
- VBS腳本寫的Windows硬件檢測(cè)工具分享
- Linux Shell腳本實(shí)現(xiàn)檢測(cè)tomcat
- Shell腳本實(shí)現(xiàn)檢測(cè)進(jìn)程是否正在運(yùn)行
- 腳本批量檢測(cè)網(wǎng)站是否存活