Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Open Port

Posted on 2013-05-31
11
Medium Priority
?
581 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 20

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 71

Accepted Solution

by:
Qlemo earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 71

Assisted Solution

by:Qlemo
Qlemo earned 2000 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 71

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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…

916 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