Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, July 17, 2013 1:01 PM
hi,
I am trying to quickly scan some servers to see what ports are open and have come across this:
$tcpobject = new-Object system.Net.Sockets.TcpClient
It works fine but the time out for a NEGATIVE answer is very long (~20seconds !), how can I control /wrap this so if a positive answer is not received in x mSecs then I will consider this a negative.
I see a sendtimeout property but I cannot figure out how to set it
$tcpobject = new-Object system.Net.Sockets.TcpClient
$tcpobject.SendTimeout = 500
and then
$socket = $tcpobject .....(cannot figure out what syntax to use?)
I also intend to try this for UDP
Roy
roys99
All replies (7)
Wednesday, July 17, 2013 1:06 PM ✅Answered | 6 votes
Look at using AsyncWaitHandle to handle the timeouts or hung attempts:
$tcpobject = new-Object system.Net.Sockets.TcpClient #Connect to remote machine's port $connect = $tcpobject.BeginConnect($computer,$port,$null,$null) #Configure a timeout before quitting - time in milliseconds $wait = $connect.AsyncWaitHandle.WaitOne(1000,$false) If (-Not $Wait) { 'Timeout'} Else { $error.clear() $tcpobject.EndConnect($connect) | out-Null If ($Error[0]) { Write-warning ("{0}" -f $error[0].Exception.Message) } Else { 'Port open!' }}
Also, I have a script that can test ports available on the script repository:
http://gallery.technet.microsoft.com/scriptcenter/97119ed6-6fb2-446d-98d8-32d823867131
Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat
Wednesday, July 17, 2013 1:44 PM ✅Answered | 1 vote
That will only work if you are sending data. In the case of checking for an open port, you are attempting a connection and the SendTimeout will not apply.
Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat
Wednesday, July 17, 2013 1:35 PM
thank you for your quick reply
can some solution be constructed using something like
$tcpobject = new-Object system.Net.Sockets.TcpClient
$tcpobject.SendTimeout = 500
and then
$socket = $tcpobject .....(cannot figure out what syntax to use?)
there seems to be some syntax iisue here I cannot figure out
tnx
roys99
Friday, July 19, 2013 3:28 AM
Hi,
Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.
If you have any feedback on our support, please click here .
Cataleya Li
TechNet Community Support
Monday, July 29, 2013 12:20 PM
can u pls explain these two lines, I do not understand what is happening here.
much obliged.
$wait = $connect.AsyncWaitHandle.WaitOne(1000,$false)
$tcpobject.EndConnect($connect) | out-Null
roys99
Monday, July 29, 2013 12:21 PM
it is very helpful - unfortunately I do not understand why.. :-)
roys99
Monday, July 29, 2013 7:09 PM
Let me know if this helps:
<#The following line sets a wait period on the asynchronous job for 1000 milliseconds. If the action completes prior to the timeout, it will be a $True value, otherwise if the timeout is reached, a $False value is returned and it is assumed to have timed out.#>$wait = $connect.AsyncWaitHandle.WaitOne(1000,$false) <#The following line closes up the asynchonous connection attempt. If an error has occurred during this connection, one will be displayed, otherwise nothing will be displayed.#>$tcpobject.EndConnect($connect) | out-Null
Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat
PowerShell Deep Dives Book