共用方式為


Multipinger with powershell

I decided to give this a go and create a multipinger with powershell... this is my first draft!

if I get time I plan to change it to use jobs instead and then format the results out... but this is a functional first draft..

 

here is the code

 

param ([parameter(mandatory=$true)] [string]$begin, [parameter(mandatory=$true)] [string]$end)

function pingip([string]$firstoct,[string]$secondoct,[string]$thirdoct,[string]$fourthoct)
{
  
 $iptotest = $firstoct + "." + $secondoct + "." + $thirdoct + "." + $fourthoct
 $test = test-connection $iptotest
     
 if ($test.count -gt 0)
 {
     $result =  "$iptotest is alive"
 }
 else
 {
  $result =  "$iptotest is dead"
 }
 
    return $result
}

 

$ErrorActionPreference = "SilentlyContinue"

$ipbegin = $begin.split(".")
$ipend = $end.split(".")
$firstoctetmatch = $false
$secondoctetmatch = $false
$thirdoctetmatch = $false
$fourthoctetmatch = $false

if ($ipbegin[0] -eq $ipend[0])
{
 $firstoctetmatch = $true
}

if ($ipbegin[1] -eq $ipend[1])
{
 $secondoctetmatch = $true
}

if ($ipbegin[2] -eq $ipend[2])
{
 $thirdoctetmatch = $true
}

if ($ipbegin[3] -eq $ipend[3])
{
 $fourthoctetmatch = $true
}

if ($firstoctetmatch -eq $true)
{

 write-host "First Octet Matches"
 $firstoctet = $ipbegin[0]
 if ($secondoctetmatch -eq $true)
 {
  write-host "Second Octet Matches"
  $secondoctet = $ipbegin[1]
  if ($thirdoctetmatch -eq $true)
  {
   write-host "Third Octect Matches"
   $thirdoctet = $ipbegin[2]
   if ($fourthoctetmatch -eq $true)
   {
    write-host "Forth Octet Matches"
    write-host "We are pinging one single IP"
    $pingip = test-connection $begin
    if ($pingip.count -gt 0)
    {
     write-host "$begin is alive"
    }
   }
   else
   {
    write-host "We are pinging a range of IP's"
    [int]$startingnumber = $ipbegin[3]
    [int]$endingnumber = $ipend[3]
    
    if (($startingnumber -eq 0) -or ($endingnumber -eq 255))
    {
     while ($startingnumber -le $endingnumber)
     {
      $fourthoctet = $startingnumber
      pingip $firstoctet $secondoctet $thirdoctet $fourthoctet
      $startingnumber++
     }
    }
    else
    {
     write-host "Invalid Number Detected in either starting number or ending number"
     write-host "Numbers cannot be 0 or 255"
     exit 1
    }
   }
  }
  else
  {
   [int]$thirdoctetstartnumber = $ipbegin[2]
   [int]$thirdoctetendnumber = $ipend[2]
   [int]$fourthoctetstartnumber = $ipbegin[3]
   [int]$fourthoctetendnumber = $ipend[3]
   
   while ($thirdoctetstartnumber -le $thirdoctetendnumber)
   {
    
    $thirdoctet = $thirdoctetstartnumber
    

    if ($thirdoctet -eq $thirdoctetendnumber)
    {
     write-host "last part of the range"
     while ($fourthoctetstartnumber -le $fourthoctetendnumber)
     {
     
      $fourthoctet = $fourthoctetstartnumber
      
      pingip $firstoctet $secondoctet $thirdoctet $fourthoctet
      $fourthoctetstartnumber++
     }
    }
    else
    {
     write-host "loads to do"
     
     while ($fourthoctetstartnumber -le 254)
     {
    
       $fourthoctet = $fourthoctetstartnumber
       
       $fourthoctetstartnumber++
     }
     $fourthoctetstartnumber = 1
    }

    $thirdoctetstartnumber++
   }

  }
 }
    else
    {
           
            [int]$secondoctetstartnumber = $ipbegin[1]
            [int]$secondoctetendnumber = $ipend[1]
            [int]$thirdoctetstartnumber = $ipbegin[2]
   [int]$thirdoctetendnumber = $ipend[2]
   [int]$fourthoctetstartnumber = $ipbegin[3]
   [int]$fourthoctetendnumber = $ipend[3]
    }
}