A script that can detect the host type in the Network

Source: Internet
Author: User
Recently, I have been writing a script to automatically detect the host type in the network. The basic function can be used to determine the host operating system type. If the host is in the domain, you can obtain the host hardware and performance parameters and determine whether a network device exists. For an O & M personnel, it is often necessary to get familiar with a strange network as soon as possible. This script is very convenient. If you have better suggestions, thank you! ######################################## ##### Author: lixiaosong #

Recently, I have been writing a script to automatically detect the host type in the network. The basic function can be used to determine the host operating system type. If the host is in the domain, you can obtain the host hardware and performance parameters and determine whether a network device exists. For an O & M personnel, it is often necessary to get familiar with a strange network as soon as possible. This script is very convenient. If you have better suggestions, thank you!

######################################## ##### Author: lixiaosong # Email: lixiaosong8706@gmail.com # For: detection/24 mask network host system type and get windows Host parameters # Version: 1.0 ####################################### ####### Param ([Parameter (Mandatory = $ true)] $ Network) $ Ip = for ($ I = 1; $ i-ile255; $ I + = 1) {"$ Network. $ I "} foreach ($ Ipaddressin $ IP) {# detect related port status $ Port3389 = 3389 | % {echo (new-objectNet.Sockets.TcpClient ). connect ("$ Ipaddress", $ _) "$ tr Ue "} 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-quietif ($ Port3389-like "$ true ") {# Server Information $ HostSN = (GWMI-ComputerName "$ Ipaddress" win32_bios ). serialNUmber $ HostFirm = (GWMI-ComputerName "$ Ipaddress" win3 2_bios ). manufacturer $ HostModel = (GWMI-ComputerName "$ Ipaddress" Win32_ComputerSystem ). model # host information $ HostName = (GWMI-ComputerName "$ Ipaddress" Win32_ComputerSystem ). DNSHostName $ DomainName = (GWMI-ComputerName "$ Ipaddress" Win32_ComputerSystem ). domain # server hardware resource information $ Freemem = (GWMI-ComputerName "$ Ipaddress" win32_OperatingSystem ). freePhysicalMemory # free physical memory $ Totalmem = (GWMI-ComputerName "$ Ipaddress" win32_OperatingSystem ). tota LVisibleMemorySize # total physical memory $ 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-co Unter-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 "-Co Unter "\ Network Interface (*) \ Bytes encoded ED/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} # obtain the Windows host parameter Write-host in the domain "====================== ========================================================== ========================================================== ======= time: $ (get-date) WINDOWS Server: $ HostName. $ DomainName IP: $ ($ IPaddress. padleft (2) Brand: $ ($ HostFirm. padleft (2) model: $ ($ HostModel. padleft (2) serial number: $ ($ HostSN. padleft (2) CPU usage: $ ($ Havecpu. padleft (8) memory usage: $ ($ Permem. padleft (13) disk read/Second: $ ($ DiskRead. padleft (8) disk write/Second: $ ($ DiskWrite. padleft (13) network transmission/Second: $ ($ NetworkSent. padleft (8) network receipt/Second: $ ($ NetworkReceive. padleft (13) Percentage of free space used by the drive letter disk "-ForegroundColorGreenforeach ($ Diskin $ Disks) {$ Size =" {0: 0. 0} GB "-f ($ Disk. size/1 GB) $ FreeSpace = "{0: 0. 0} GB "-f ($ Disk. freeSpace/1 GB) $ Used = ([int64] $ Disk. size-[int64] $ Disk. freespace) $ SpaceUsed = "{0: 0. 0} GB "-f ($ Used/1 GB) $ Percent =" {0: 0. 0} % "-f ($ Used x 100/$ Disk. size) $ n = 3Write-Host "" $ Disk. deviceid. padRight ($ n)-no-ForegroundColorGreen $ n = 10Write-Host $ Size. padleft ($ n)-no-ForegroundColorGreenWrite-Host $ FreeSpace. padleft ($ n)-no-ForegroundColorGreenWrite-Host $ SpaceUsed. padleft ($ n)-no-ForegroundColorGreenWrite-Host $ Percent. padleft ($ n)-ForegroundColorGreen} # determine the linux host if ($ port22-like "$ true ") {write-host "======================================== ========================================================== ====================================================== Server: $ IPaddress open port: "22" may be a "linux" host "-ForegroundColorYellow} # determine the network device if ($ port23-like" $ true ") {Write-host "======================================== ========================================================== ====================================================== Server: $ Ipaddress open port: "23" may be a "network" device "-ForegroundColorCyan} # The host does not exist if ($ Pingtest-like" $ False ") {Write-host "======================================== ========================================================== ====================================================== Server: $ Ipaddress this host does not exist "-ForegroundColorRed }}

Example:

1. Save the script to c :\

2. Run powershell to execute ps c: \>. \ test. ps1 10.7.2 # enter the first three digits of the network.

Ps c: \>. \ test. ps1 10.7.2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.