Sha256: e70ee3b0a9b264b71fa55405afa79dcabe90f73daa236c901dbf8a2c089ae707
Contents?: true
Size: 1.43 KB
Versions: 467
Compression:
Stored size: 1.43 KB
Contents
function IsRemotePortListening{ param( [string]$hostname, [string]$proto="tcp", [int]$port, [int]$timeout) If ($proto -eq "tcp") { $tcpobject = new-Object system.Net.Sockets.TcpClient $connect = $tcpobject.BeginConnect($hostname,$port,$null,$null) $wait = $connect.AsyncWaitHandle.WaitOne($timeout * 1000,$false) #If timeout If(!$wait) { $tcpobject.Close() return $false } else{ $result = $tcpobject.Connected $tcpobject.Close() return $result } } elseif ($proto -eq "udp") { $udpobject = new-Object system.Net.Sockets.Udpclient $udpobject.client.ReceiveTimeout = $timeout * 1000 $udpobject.Connect($hostname,$port) $a = new-object system.text.asciiencoding $byte = $a.GetBytes("$(Get-Date)") [void]$udpobject.Send($byte,$byte.length) $remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) try{ #Blocks until a message returns on this socket from a remote host. $receivebytes = $udpobject.Receive([ref]$remoteendpoint) [string]$returndata = $a.GetString($receivebytes) If ($returndata) { $udpobject.close() return $true } else{ return $false } } catch{ return $false } } else{ throw "Protocol ${proto} not supported" } }
Version data entries
467 entries across 467 versions & 3 rubygems