Solved

Powershell Open Port

Posted on 2013-05-31
11
538 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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 70

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 70

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

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...
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

751 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