A-A+

一个可以检测网络内主机类型的脚本

2015年11月25日 站长资讯 一个可以检测网络内主机类型的脚本已关闭评论

最近一直在写一个自动检测网络内主机类型的脚本。基本功能可以实现判断主机操作系统类型,如果是域内的主机可以获取主机的硬件参数和性能参数,并判断是否存在网络设备。对一个运维人员来说往往需要尽快熟悉一个陌生的网络。所以这个脚本就很方便了,如果有更好的建议欢迎指正感谢!

  1. ############################################  
  2. #Author:Lixiaosong  
  3. #Email:lixiaosong8706@gmail.com  
  4. #For:检测/24掩码网络内主机系统类型并获取windows主机参数  
  5. #Version:1.0  
  6. ##############################################  
  7. Param(  
  8.       [Parameter(Mandatory=$true)]$Network  
  9. )  
  10. $Ip=for ($i = 1; $i -ile 255; $i += 1){"$Network.$i"}  
  11. foreach ($Ipaddress in $IP){  
  12.     #检测相关端口状态  
  13.     $Port3389=3389 | %{ echo ((new-object Net.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null  
  14.     $Port22=22 | %{ echo ((new-object Net.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null  
  15.     $Port23=23 | %{ echo ((new-object Net.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null  
  16.     $Pingtest=Test-connection -ComputerName $IPaddress  -quiet   
  17.     if ($Port3389 -like "$true" -and $Port443 -like "$true"){  
  18.         #服务器信息  
  19.         $HostSN=(GWMI -ComputerName "$Ipaddress"  win32_bios).SerialNUmber  
  20.         $HostFirm=(GWMI -ComputerName "$Ipaddress" win32_bios).Manufacturer  
  21.         $HostModel=(GWMI -ComputerName "$Ipaddress"  Win32_ComputerSystem).Model  
  22.         #主机信息  
  23.         $HostName=(GWMI -ComputerName "$Ipaddress"  Win32_ComputerSystem).DNSHostName  
  24.         $DomainName=(GWMI -ComputerName "$Ipaddress" Win32_ComputerSystem).Domain  
  25.         #服务器硬件资源信息  
  26.         $Freemem=(GWMI -ComputerName "$Ipaddress" win32_OperatingSystem).FreePhysicalMemory#空余物理内存  
  27.   
  28.   
  29.         $Totalmem=(GWMI -ComputerName "$Ipaddress" win32_OperatingSystem).TotalVisibleMemorySize#总物理内存  
  30.         $cpu=((get-counter -ComputerName "$IPaddress" -counter "\processor(_total)\% processor time").CounterSamples|where {$_.InstanceName -eq "_total" }).CookedValue  
  31.         $DiskRead=" {0:0.0} KB" -f ($(((get-counter -ComputerName "$Ipaddress" -counter "\LogicalDisk(_total)\Disk Read Bytes/sec").CounterSamples|where {$_.InstanceName -eq "_total" }).CookedValue) / 1KB)  
  32.         $DiskWrite="{0:0.0} KB" -f ($(((get-counter -ComputerName "$Ipaddress" -counter "\LogicalDisk(_total)\Disk Write Bytes/sec").CounterSamples|where {$_.InstanceName -eq "_total" }).CookedValue) /1KB)  
  33.         $NetworkSent=" {0:0.0} KB" -f ($((Get-Counter -ComputerName "$Ipaddress" -Counter "\Network Interface(*)\Bytes Sent/sec").CounterSamples|%{$_.CookedValue}|sort|select -last 1) / 1KB)   
  34.         $NetworkReceive=" {0:0.0} KB" -f ($((Get-Counter -ComputerName "$IPaddress" -Counter "\Network Interface(*)\Bytes Received/sec").CounterSamples|%{$_.CookedValue}|sort|select -last 1) / 1KB)  
  35.         $Havecpu = "{0:0.0} %" -f $cpu  
  36.         $Permem="{0:0.0} %" -f ((($Totalmem-$Freemem)/$Totalmem)*100)  
  37.         $Disks = GWMI -ComputerName "$IPaddress" win32_logicaldisk|?{$_.drivetype -eq 3}  
  38.     #获取域内Windows主机参数  
  39.     Write-host "  
  40.     =====================  

时间:$(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))

盘符 盘总空间 空闲空间 使用空间 使用百分比" -ForegroundColor Green

  1. foreach ($Disk in $Disks){  
  2.     $Size = "{0:0.0} GB" -f ($Disk.Size / 1GB )  
  3.     $FreeSpace = "{0:0.0} GB" -f ($Disk.FreeSpace / 1GB)  
  4.     $Used = ([int64]$Disk.size - [int64]$Disk.freespace)  
  5.     $SpaceUsed = "{0:0.0} GB" -f ($Used / 1GB)  
  6.   
  7.   
  8.     $Percent ="{0:0.0} %" -f ($Used * 100 / $Disk.Size)      
  9.      $n=3  
  10. Write-Host  "  "$Disk.deviceid.PadRight($n) -no -ForegroundColor Green  
  11.      $n=10  
  12. Write-Host  $Size.Padleft($n) -no -ForegroundColor Green  
  13. Write-Host  $FreeSpace.Padleft($n) -no -ForegroundColor Green  
  14. Write-Host  $SpaceUsed.Padleft($n) -no -ForegroundColor Green  
  15. Write-Host  $Percent.Padleft($n)  -ForegroundColor Green  
  16. }  
  17. }  
  18. 判断linux主机  
  19. if ($port22 -like "$true"){  
  20. write-host "  
  21. =======================  
  22. 服务器:$IPaddress 开放端口:"22" 可能是一台是"linux"主机" -ForegroundColor Yellow   
  23. }  
  24. #判断网络设备  
  25. if ($port23 -like "$true"){  
  26. Write-host "  
  27. ========================  
  28. 服务器:$Ipaddress 开放端口:"23" 可能是一台"网络"设备"   -ForegroundColor Cyan  
  29. }  
  30. #主机不存在  
  31. if ($Pingtest -like "$False"){  
  32. Write-host "  
  33. =================  
  34. 服务器:$Ipaddress 此主机不存在" -ForegroundColor  Red  
  35. }  

使用方法举例:

1 将脚本保存至c:\

2 运行powershell 执行PS C:\> .\test.ps1 10.7.2 #只需输入网络的前三位

PS C:\> .\test.ps1 10.7.2

标签:

评论已关闭!