Created
May 4, 2017 15:18
-
-
Save PrateekKumarSingh/a4625960f110444536ef3ce72507c6b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test-Port -Source '127.0.0.1' -Destination '10.196.11.206','10.196.11.206','10.196.11.206','123.234.234.3' ` | |
-Port 3389,20, 21 -Verbose | ft -AutoSize | |
#-Iterate | Export-Csv portstatus.csv -NoTypeInformation | |
#Test-PortConnectivity '127.0.0.1' 'dc1' 57766 -Protocol UDP -Iterate | |
#Test-PortConnectivity 'localhost' 'dc2' 51753 -Protocol UDP | |
#Test-PortConnectivity -Source $EUCAS -Destination $EUMBX -Port 135 -Iterate | |
#Test-PortConnectivity -Source 'localhost' -Destination '127.0.0.1' -Port 135 -Iterate -protocol TCP | |
Function Test-Port | |
{ | |
[cmdletbinding()] | |
Param | |
( | |
[Parameter(Position=0)] $Source = $(Hostname), | |
[Parameter(Mandatory=$true,Position=1)] $Destination, | |
[Parameter(Mandatory=$true,Position=2)] | |
[ValidateScript({ | |
If($_ -match "^[0-9]+$"){$True} | |
else{Throw "A port should be a numeric value, and $_ is not a valid number"} | |
})] $Port, | |
[Parameter(Position=3)][ValidateSet('TCP','UDP')] $Protocol = 'TCP', | |
[Switch] $Iterate | |
) | |
Begin{} | |
Process | |
{ | |
# If $source is a localhost, invoke command is not required and we can test port, withhout credentials | |
If($Source -like "127.*" -or $source -like "*$(hostname)*" -or $Source -like 'localhost') | |
{ | |
Do | |
{ | |
PSTelnet $Destination $Port $Protocol; | |
Start-Sleep -Seconds 1 #Initiate sleep to slow down Continous telnet | |
}While($Iterate) | |
} | |
Else #Prompt for credentials when Source is not the local machine. | |
{ | |
Get-Credential -Message "Provide Admin credentials to Telnet from [Remote source] to [Remote destination] machine(s)" -OutVariable Credentials | |
If($Credentials) | |
{ | |
Do | |
{ | |
Foreach($Item in $Source) | |
{ | |
Invoke-command -ComputerName $Item ` | |
-Credential $Credentials ` | |
-ScriptBlock ${Function:PSTelnet} ` | |
-ArgumentList $Destination,$Port,$Protocol | |
} | |
# Sleep to slow down Continous telnet | |
Start-Sleep -Seconds 1 | |
}While($Iterate) | |
} | |
} | |
} | |
end{} | |
} | |
Function PSTelnet | |
{ | |
[Alias("Tn")] | |
[Cmdletbinding()] | |
Param( | |
[String[]] $Destination, | |
[Int[]] $port, | |
[string[]] $Protocol | |
) | |
Begin | |
{ | |
$Source = (hostname).toupper() | |
} | |
Process | |
{ | |
Foreach($Target in $Destination) | |
{ | |
Foreach($CurrentPort in $Port) | |
{ | |
Write-Verbose "Testing $Protocol port $CurrentPort from $Source to $Target" | |
$HostStatus = (QuickPing -Computername '127.0.0.1').status | |
If($Protocol -eq 'TCP') | |
{ | |
Try | |
{ | |
$TCPClient = New-Object System.Net.Sockets.TCPClient | |
$Connection = $TCPClient.BeginConnect('127.0.0.1',20, $null, $null) | |
$wait = $Connection.AsyncWaitHandle.WaitOne(5000,$false) | |
$TCPClient.EndConnect($Connection) | |
If($PortStatus) | |
{ | |
#Write-host "$((hostname).toupper()) connected to $($Target.toupper()) on $Protocol port : $currentPort " -back green -ForegroundColor White | |
''|select @{n='Source';e={$Source}},` | |
@{n='Destination';e={$Target.toupper()}},` | |
@{n='Protocol';e={$Protocol}},` | |
@{n='Port';e={$CurrentPort}},` | |
@{n='Open';e={$true}}, ` | |
@{n='HostUp?';e={If($HostStatus -eq 'success'){$True}else{$false}}} | |
} | |
} | |
Catch | |
{ | |
#Write-host "$((hostname).toupper()) Not connected to $($Target.toupper()) on $Protocol port : $currentPort" -back red -ForegroundColor white | |
''|select @{n='Source';e={$Source}},` | |
@{n='Destination';e={$Target.toupper()}},` | |
@{n='Protocol';e={$Protocol}},` | |
@{n='Port';e={$CurrentPort}},` | |
@{n='Open';e={$false}}, ` | |
@{n='HostUp?';e={If($HostStatus -eq 'success'){$True}else{$false}}} | |
} | |
Finally | |
{ | |
$TCPClient.Close() | |
} | |
} | |
Else | |
{ | |
#Create object for connecting to port on computer | |
$UDPClient = new-Object system.Net.Sockets.Udpclient | |
#Set a timeout on receiving message, to avoid source machine to Listen forever. | |
$UDPClient.client.ReceiveTimeout = 5000 | |
#Datagrams must be sent with Bytes, hence the text is converted into Bytes | |
$ASCII = new-object system.text.asciiencoding | |
$Bytes = $ASCII.GetBytes("Hi") | |
#UDP datagram is send | |
[void]$UDPClient.Send($Bytes,$Bytes.length,$Target,$Port) | |
$RemoteEndpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) | |
Try | |
{ | |
#Waits for a UDP response until timeout defined above | |
$RCV_Bytes = $UDPClient.Receive([ref]$RemoteEndpoint) | |
$RCV_Data = $ASCII.GetString($RCV_Bytes) | |
If($RCV_Data) | |
{ | |
#Write-host "$((hostname).toupper()) connected to $($Target.toupper()) on $Protocol port : $currentPort " -back green -ForegroundColor White | |
''|select @{n='Source';e={$Source}},` | |
@{n='Destination';e={$Target.toupper()}},` | |
@{n='Protocol';e={$Protocol}},` | |
@{n='Port';e={$CurrentPort}},` | |
@{n='Open';e={$true}}, ` | |
@{n='HostUp?';e={If($HostStatus -eq 'success'){$True}else{$false}}} | |
} | |
} | |
catch | |
{ | |
#If the UDP recieve is timed out it's inferred that no response was received. | |
#Write-host "$((hostname).toupper()) Not connected to $($Target.toupper()) on $Protocol port : $currentPort " -back red -ForegroundColor White | |
''|select @{n='Source';e={$Source}},` | |
@{n='Destination';e={$Target.toupper()}},` | |
@{n='Protocol';e={$Protocol}},` | |
@{n='Port';e={$CurrentPort}},` | |
@{n='Open';e={$false}}, ` | |
@{n='HostUp?';e={If($HostStatus -eq 'success'){$True}else{$false}}} | |
} | |
Finally | |
{l | |
#Disposing Variables | |
$UDPClient.Close() | |
$RCV_Data=$RCV_Bytes=$null | |
} | |
} | |
} | |
} | |
} | |
end{} | |
} | |
# Though we have Test-Connection cmdlet, but that don't have a Timeout option in it. | |
# Which makes running the script for multiple machines somewhat slower, as it waits for default timeout | |
# As a workaround, instantiating System.Net class give us an option to specify the TimeOut for ICMP requests, hence this function | |
Function QuickPing | |
{ | |
Param( | |
[String] $Computername | |
) | |
[int]$timeout = 80 | |
[switch]$resolve = $true | |
[int]$TTL = 128 | |
[switch]$DontFragment = $false | |
[int]$buffersize = 32 | |
$options = new-object system.net.networkinformation.pingoptions | |
$options.TTL = $TTL | |
$options.DontFragment = $DontFragment | |
$buffer=([system.text.encoding]::ASCII).getbytes("hi"*$buffersize) | |
$Ping = New-Object system.net.networkinformation.ping | |
$ping.Send($Computername,$timeout,$buffer,$options) | |
$ping.Dispose() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment