Solved

Powershell Open Port

Posted on 2013-05-31
11
529 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 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
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…

762 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