/usr/lib/ruby/vendor_ruby/specinfra/backend/powershell/support/is_remote_port_listening.ps1 is in ruby-specinfra 2.66.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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"
}
}
|