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

简介:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
############################################
#Author:Lixiaosong
#Email:lixiaosong8706@gmail.com
#For:检测/24掩码网络内主机系统类型并获取windows主机参数
#Version:1.0
##############################################
Param (
       [ Parameter ( Mandatory = $true )] $Network
)
$Ip = for  ( $i  = 1;  $i  -ile  255;  $i  += 1){ "$Network.$i" }
foreach  ( $Ipaddress  in  $IP ){
     #检测相关端口状态
     $Port3389 =3389 | %{ echo (( new-object  Net.Sockets.TcpClient).Connect( "$Ipaddress" , $_ ))  "$true" } 2> $null
     $Port22 =22 | %{ echo (( new-object  Net.Sockets.TcpClient).Connect( "$Ipaddress" , $_ ))  "$true" } 2> $null
     $Port23 =23 | %{ echo (( new-object  Net.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  -last  1) / 1KB)
         $NetworkReceive = " {0:0.0} KB"  -f  ($(( Get-Counter  -ComputerName  "$IPaddress"  -Counter  "\Network Interface(*)\Bytes Received/sec" ).CounterSamples|%{ $_ .CookedValue} |sort |select  -last  1) / 1KB)
         $Havecpu  "{0:0.0} %"  -f  $cpu
         $Permem = "{0:0.0} %"  -f  ((( $Totalmem - $Freemem )/ $Totalmem )*100)
         $Disks  = GWMI  -ComputerName  "$IPaddress"  win32_logicaldisk|?{ $_ .drivetype  -eq  3}
     #获取域内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))
     盘符   盘总空间  空闲空间  使用空间  使用百分比"  -ForegroundColor  Green
     foreach  ( $Disk  in  $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  -ForegroundColor  Green
          $n =10
     Write-Host   $Size .Padleft( $n )  -no  -ForegroundColor  Green
     Write-Host   $FreeSpace .Padleft( $n )  -no  -ForegroundColor  Green
     Write-Host   $SpaceUsed .Padleft( $n )  -no  -ForegroundColor  Green
     Write-Host   $Percent .Padleft( $n )   -ForegroundColor  Green
     }
     }
    #判断linux主机
     if  ( $port22  -like  "$true" ){
     write-host  "
     ================================================================================================================
     服务器:$IPaddress 开放端口:" 22 " 可能是一台是" linux "主机"  -ForegroundColor  Yellow
     }
     #判断网络设备
     if  ( $port23  -like  "$true" ){
     Write-host  "
     ================================================================================================================
     服务器:$Ipaddress 开放端口:" 23 " 可能是一台" 网络 "设备"    -ForegroundColor  Cyan
     }
     #主机不存在
     if  ( $Pingtest  -like  "$False" ){
     Write-host  "
     ================================================================================================================
     服务器:$Ipaddress 此主机不存在"  -ForegroundColor   Red
     }
   }

使用方法举例:

1 将脚本保存至c:\

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

PS C:\> .\test.ps1 10.7.2

wKioL1LT-mSCxYLkAAUAJZYM-LY607.jpg





本文转自handsome7038 51CTO博客,原文链接:http://blog.51cto.com/lixiaosong/1351351

相关文章
|
1月前
|
网络协议 Linux Shell
搭建虚拟机的网络布局类型和配置操作
搭建虚拟机的网络布局类型和配置操作
|
1月前
|
机器学习/深度学习 算法 计算机视觉
基于yolov2深度学习网络的火焰烟雾检测系统matlab仿真
基于yolov2深度学习网络的火焰烟雾检测系统matlab仿真
|
1月前
|
机器学习/深度学习 算法 计算机视觉
基于yolov2深度学习网络的视频手部检测算法matlab仿真
基于yolov2深度学习网络的视频手部检测算法matlab仿真
|
9天前
|
安全 网络协议 物联网
计算机网络基础教程:类型
【4月更文挑战第5天】
24 2
 计算机网络基础教程:类型
|
27天前
|
存储 Shell Linux
【Shell 命令集合 网络通讯 】⭐Linux 显示当前系统的主机名和操作系统类型 uuname命令 使用教程
【Shell 命令集合 网络通讯 】⭐Linux 显示当前系统的主机名和操作系统类型 uuname命令 使用教程
28 0
|
28天前
|
弹性计算 安全 关系型数据库
rds网络类型选取
云数据库RDS的网络类型选择包括经典网络、专有网络(VPC)、公共网络和私有网络。推荐使用VPC以实现高安全隔离,私有网络增加安全性且节省公网费用。根据业务需求和安全策略选择,如需内部访问选择同一VPC,外部访问则需公网并配以严格安全措施。最新阿里云RDS建议使用VPC并结合安全组、网络ACL和白名单保障安全,通过CEN实现跨地域通信。
16 7
|
1月前
ping 测试主机之间网络连通性
ping 测试主机之间网络连通性。
42 1
|
2月前
|
机器学习/深度学习 算法 计算机视觉
m基于Yolov2深度学习网络的喝水行为检测系统matlab仿真,带GUI界面
m基于Yolov2深度学习网络的喝水行为检测系统matlab仿真,带GUI界面
18 0
|
2月前
|
机器学习/深度学习 数据采集 监控
基于yolov2深度学习网络的车辆检测算法matlab仿真,包括白天场景和夜晚场景
基于yolov2深度学习网络的车辆检测算法matlab仿真,包括白天场景和夜晚场景
|
2月前
|
机器学习/深度学习 数据采集 算法
基于yolov2深度学习网络的血细胞检测算法matlab仿真
基于yolov2深度学习网络的血细胞检测算法matlab仿真

热门文章

最新文章