Is It Unix or Windows ?

 

So someone hands you list of servers and they want to know everything there is to know about said servers by COB today.

 
Time for a script! But wait, when you run your script, you get several servers that do not reply or give you RPC failures.
Are they dead? Disconnected? or are they Unix?
 
Vadims Podāns on the PowerShell google group had this to say,
Well, here is a quick and dirty way to find out if they are Unix or Windows.
You can simply ping remote the host.
By default all *nix (Linux) reply with TTL=64 and Windows hosts reply with
TTL=128 this helps only determine the host is UNIX or not and you will unable to
determine Unix version. Be aware that at ICMP packet passage through each router ttl decreases by 1.
 
So using Vadims information, I came up with this Posh script.
#<—————- Begin ———————————————–>
$ping = New-Object System.Net.NetworkInformation.Ping
$computerlist = Get-Content ‘c:scriptsServers.txt’
foreach ($srv in $computerlist)
{
        $reply = $ping.Send($srv)
        if ($reply.status -eq "Success")
        {
                if ($reply.Options.ttl -le 64)
                {
                Write-Host $srv " probably is a Unix host"
                        }
                Else
                        { if ($reply.Options.ttl -ge 65)
                                {
                        Write-Host $srv " probably is a Windows host"
                                }
                }
        }
        Else { Write-Host $srv " Does Not Ping"
                 }
}
#<———————– End Script ————————————-­—–>
 
So I was getting some inconsistent results on my Vista work station, so I re-wrote the script like so;
 
  $computerlist = Get-Content ‘c:scriptsServers.txt’
 foreach ($srv in $computerlist)
 {
$response = Get-WmiObject -query "Select * From Win32_PingStatus Where Address = ‘$srv’"
 
  if( ($response -eq $null) -or ($response.StatusCode -ne 0)) {
                Write-Host $srv " Does Not Ping"
        } else { if ($response.TimeToLive -le 64)
                       {
                Write-Host $srv " probably is a Unix host"
                        }
                Else {
                        Write-Host $srv " probably is a Windows host" 
                       }
                }
        }
 
Advertisements

2 thoughts on “Is It Unix or Windows ?

  1. Hi!
    Thanks for mention!
    Can you correct my surname in post entry, please?
     
    and about script:
     
    if ($reply.Options.ttl -ge 65) – is this so neccessary? In first IF statement you check that TTL is less or equal 64. Threfore it can be simplier:
     
    if ($reply.Options.ttl -le 64) {
                    Write-Host $srv " probably is a Unix host" 
             } Else {
                    Write-Host $srv " probably is a Windows host"                } }
     
    I know that it’s not absolutely correctly, but I think if we have successfull ping, then TTL will be returned correctly (integer data)
    —————————————————————–
    For future script extension may be would be better to use SWITCH statement instead IF statements? For example:
     
    switch ($reply.status) {
    "Success" { if ($reply.Options.ttl -le 64) {Write-Host $srv " probably is a Unix host"} else {Write-Host $srv " probably is a Windows host"}}
    default {Write-Host $srv " Does Not Ping"}
    }
     
    with SWITCH we can simply add other ping result and associated with it appropriate code (for example, if no route to host found or something similar).
     
    Thanks!

  2. And what about your script rewriting to function? It would be more usable for others:
    function Get-HostType { Begin {$ping = New-Object System.Net.NetworkInformation.Ping} Process {  $srv = $_  $reply = $ping.Send($_)  switch ($reply.Status) {   "Success" {if ($reply.Options.ttl -le 64) {       Write-Host $srv " probably is a Unix host"      } else {Write-Host $srv " probably is a Windows host"}     }  default {Write-Host $srv " Does Not Ping"}  } }}
     
    and usage will be: gc computernames.txt | Get-HostType
     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s