[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8019
  • Last Modified:

500: Simply stop port listening

Is there command that I can type in the command prompt to stop a port from listening?? Not permanently.
I have a script, which when run, opens a few ports for listening....but it doesn't close them ever and I can't change the script so that's why I want to do it from the cammand line.


Many thanks in advance
0
smotbd
Asked:
smotbd
  • 3
  • 2
  • 2
  • +1
1 Solution
 
2266180Commented:
provided that you are using ptables:

iptables -I INPUT -p tcp -m tcp --dport <the_port> -j DROP

and to remove it
iptables iD INPUT 1

this will still leave the port listening, but it will prevent connections to it.
anoter way would be to open some bogus program to listen on those ports, but that might raise errors in the script if it hadn't been written to deal with such issues.
0
 
smotbdAuthor Commented:
I don't and won't use iptables sorry.

Is there not a simple command or script that can be run?? It's only a setting somewhere!

Thanks
0
 
2266180Commented:
well, if you are so sure that it is only a setting somewhere, what is thet setting? or where is somewhere?

you cannot have a OS setting that says that an applicaiton cannot bind to a specific port.
you either do that from the application or you block the access to that port for other applications. you don't want to do either of that.
and may I know:
- why is it a problem that the script binds to some ports?
- why don't you want to use iptables? or other kernal-firewalling module/app?
- why don't you want to modify the script?

maybe the problem is somewhere else and thus the solution is not even close to denying access to a port.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nociSoftware EngineerCommented:
If you open a port it is listening, if you close a port it isn't

If you can't change the regime on opening/closing then blocking the the only other option.
There is no other option to enable/disable listening.
0
 
slyongCommented:
may be you should post your script here?
0
 
smotbdAuthor Commented:
The script is a PHP script which simply acts as a server listening on particular ports.
I start this PHP script via the url on a web browser..... If I click on the cross on my web browser, the script stops running. I thought then that the ports would stop listening since the script has stopped. The application has stopped yet the ports are still listening. How?
So I concluded this: when the application is ran, the functions socket_create/listen must set a value of some sort to set these ports to listening as oppose to not listening!! So then, when I stop the script nothing changes. I know that socket_close or what have you will close this sockeet.....but I just wondered if there was a command that one could enter in to the command line to stop these ports set to listening.

After all, computers, of course, are just electronic machines. They don't know about Linux and
menus, they don't know about programs or instructions, and they don't even know about 1s and 0s. All
that is really going on is that voltage is being measured at various places on an integrated circuit. I thought that one could do anything under linux and all I want to do is stop a port listening!!

Anyway,, it's just a question out of curiosity  more than anything....but if I'm talking nonsence then I apologise!!
0
 
slyongCommented:
I would suggest you detect the browser close event and close the socket properly.  There are some information here: http://www.linuxforums.org/forum/misc/1871-terminating-open-ports.html that state using lsof -i you can find out the process associated with the port.  Not sure if that will work with PHP.
0
 
smotbdAuthor Commented:
yeh i do need to close ports properly............................
 lsof -i detected the ports left open....and their pid so I killed them thanks!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now