Advertisement
timsstuff

TCPing

Sep 4th, 2015
1,785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function tcping {
  2.        param (
  3.               [Parameter(Position=0)][string] $Server,
  4.               [Parameter(Position=1)][string] $Port,
  5.               [Parameter(Position=2)][int] $TimeOut = 2
  6.        )
  7.        
  8.        if($Server -eq "") { $Server = Read-Host "Server" }
  9.        if($Port -eq "") { $Port = Read-Host "Port" }
  10.        if($Timeout -eq "") { $Timeout = 2 }
  11.        [int]$TimeOutMS = $TimeOut*1000
  12.        $IP = [System.Net.Dns]::GetHostAddresses($Server)
  13.        $Address = [System.Net.IPAddress]::Parse($IP)
  14.        $Socket = New-Object System.Net.Sockets.TCPClient
  15.        
  16.        Write-Host "Connecting to $Address on port $Port" -ForegroundColor Cyan
  17.        Try {
  18.               $Connect = $Socket.BeginConnect($Address,$Port,$null,$null)
  19.        }
  20.        Catch {
  21.               Write-Host "$Server is NOT responding on port $Port" -ForegroundColor Red
  22.               Write-Host ""
  23.         Return $false
  24.               Exit
  25.        }
  26.        
  27.        Start-Sleep -Seconds $TimeOut
  28.        
  29.        if ( $Connect.IsCompleted )
  30.        {
  31.               $Wait = $Connect.AsyncWaitHandle.WaitOne($TimeOutMS,$false)                
  32.               if(!$Wait)
  33.               {
  34.                      $Socket.Close()
  35.                      Write-Host "$Server is NOT responding on port $Port" -ForegroundColor Red
  36.             Return $false
  37.               }
  38.               else
  39.               {
  40.                      Try {
  41.                            $Socket.EndConnect($Connect)
  42.                            Write-Host "$Server IS responding on port $Port" -ForegroundColor Green
  43.                 Return $true
  44.                      }
  45.                      Catch { Write-Host "$Server is NOT responding on port $Port" -ForegroundColor Red }
  46.                      $Socket.Close()
  47.             Return $false
  48.               }
  49.        }
  50.        else
  51.        {
  52.               Write-Host "$Server is NOT responding on port $Port" -ForegroundColor Red
  53.         Return $false
  54.        }
  55.        Write-Host ""
  56.  
  57. }
  58.  
  59. function waitrdp($server) {
  60.     while((tcping -server $server -port 3389) -eq $false) {start-sleep -s 5}
  61.     if(Test-Path "D:\Media\Sounds\Wav\Windows\TBONEWAH.WAV") {
  62.         $sound = new-Object System.Media.SoundPlayer
  63.         $sound.SoundLocation="D:\Media\Sounds\Wav\Windows\TBONEWAH.WAV"
  64.         $sound.Play()
  65.     }
  66. }
  67.  
  68. function waithttp($server) {
  69.     while((tcping -server $server -port 80) -eq $false) {start-sleep -s 5}
  70.     if(Test-Path "D:\Media\Sounds\Wav\Windows\TBONEWAH.WAV") {
  71.         $sound = new-Object System.Media.SoundPlayer
  72.         $sound.SoundLocation="D:\Media\Sounds\Wav\Windows\TBONEWAH.WAV"
  73.         $sound.Play()
  74.     }
  75. }
  76.  
  77. function waitssl($server) {
  78.     while((tcping -server $server -port 443) -eq $false) {start-sleep -s 5}
  79.     if(Test-Path "D:\Media\Sounds\Wav\Windows\TBONEWAH.WAV") {
  80.         $sound = new-Object System.Media.SoundPlayer
  81.         $sound.SoundLocation="D:\Media\Sounds\Wav\Windows\TBONEWAH.WAV"
  82.         $sound.Play()
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement