Solved

500: Simply stop port listening

Posted on 2006-07-21
8
8,005 Views
Last Modified: 2009-04-07
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
Comment
Question by:smotbd
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 28

Expert Comment

by:ciuly
ID: 17152850
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
 

Author Comment

by:smotbd
ID: 17153670
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
 
LVL 28

Expert Comment

by:ciuly
ID: 17153817
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
 
LVL 39

Expert Comment

by:noci
ID: 17154142
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 24

Expert Comment

by:slyong
ID: 17161392
may be you should post your script here?
0
 

Author Comment

by:smotbd
ID: 17161634
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
 
LVL 24

Accepted Solution

by:
slyong earned 125 total points
ID: 17161676
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
 

Author Comment

by:smotbd
ID: 17176478
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

20 Experts available now in Live!

Get 1:1 Help Now