Solved

Powershell Open Port

Posted on 2013-05-31
11
510 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 18

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 68

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
 
LVL 1

Author Comment

by:jhakie
ID: 39211443
that's what I was thinking. Any idea on how I can create 'dummy' traffic?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 68

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 68

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

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now