Solved

Powershell Open Port

Posted on 2013-05-31
11
525 Views
Last Modified: 2014-09-30
I am trying to write a PS script that will open a TCP port for an extended period of time. Below is my working script. It works fine. My issue is, if I set the: "start-Sleep" to like 3600 seconds is when I run into an issue.

The Script continues to sleep for the full 3600 seconds but after a random amount of time, could be 15 minutes, 30 minutes the server (or something on the server) shuts the port mid script and I can't telnet into the port (but script is still running).

I have disabled the Windows Firewall to eliminate this as the issue.

I am running this on Windows Server 2008 R2 SP1. Thoughts?



$Listener = [System.Net.Sockets.TcpListener]33335

$listener.start()

write-host "Waiting for a connection on port $port..."

write-host "Port 33335 Open!"

$listener.AcceptTcpClient()

start-Sleep -s 60

$listener.Stop()

write-host "Port 33335 Closed!"
0
Comment
Question by:jhakie
  • 5
  • 3
11 Comments
 
LVL 19

Expert Comment

by:Peter Hutchison
ID: 39210945
Really you should be polling or listening the port for any incoming traffic in a loop rather than just go into sleep mode.

Some sample code here:
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener%28v=vs.100%29.aspx
0
 
LVL 1

Author Comment

by:jhakie
ID: 39210960
I need to attempt to keep the port open for a certain amount of time. My load balancer will be monitoring this port to determine if it needs to failover or not.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39211310
AcceptTcpClient() will only accept one connection, and only if it is pending. The connection is then not used for a very long time (no traffic), and TCP shuts down the port because of timeouts. This is to make sure crashed listeners do not allocate resources forever.

You will need to create traffic to keep the port open.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Author Comment

by:jhakie
ID: 39211443
that's what I was thinking. Any idea on how I can create 'dummy' traffic?
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 39211487
Usually the socket should send a repetive ACK for the last packet received. This is, AFAIK, a option of the socket, and then set up according to registry values on Windows; see http://technet.microsoft.com/en-us/library/cc957549.aspx for the latter, http://blogs.technet.com/b/nettracer/archive/2010/06/03/things-that-you-may-want-to-know-about-tcp-keepalives.aspx for how to enable keep-alives.
0
 
LVL 1

Author Comment

by:jhakie
ID: 39211842
This might be good. I have made the registry change on my dev box and so far so good. I will follow up tomorrow if successful.

Thanks!
0
 
LVL 1

Author Comment

by:jhakie
ID: 39217127
This doesn't seem to work either. The port gets closed down after a few hours.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39217452
Is there are another firewall somewhere on the way?
The keep-alives have to keep the connection alive and set up in any session-related intermediate device (like hardware firewalls). If that does not help, I don't know what should be able to.

On another note, it is quite unusual to keep a connection open for several hours without exchanging any traffic. In such cases rebuilding a broken connection is a necessary feature for client/server software.
0
 
LVL 1

Author Comment

by:jhakie
ID: 39219089
I am trying to monitor the port over a site-to-site VPN which could be the issue.

What I think I am going to do is create a site in IIS7 that points to no where and then bind to the port I am looking to have open. Then use a PS script to start and stop the site when I need it. That seemed to work last night while testing.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question