Is there any command I can run from bash that will tell me whether a port is already open?
|
Use "netstat" to check the presently using ports. netstat -antp Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 xxx.xxx.xxx.xxx 0.0.0.0:* LISTEN 16297/named tcp 0 0 xxx.xxx.xxx.xxx:53 0.0.0.0:* LISTEN 16297/named tcp 0 0 xxx.xxx.xxx.xxx:53 0.0.0.0:* LISTEN 16297/named tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 16297/named |
|||||
|
|
This (netstat) is the fastest solution...
...but this gives you more control (at the cost of speed (sometimes a lot of speed))...
The above example for example shows you all the TCP ports open and in the |
|||
|
|
|
All good answers. However you don't mention if you are logged onto the computer in question. ;P If not, then nmap is your friend. for starters try:
amap is also a good choice which will also attempt to guess server software by grabbing banner pages. for starters try:
|
||||
|
|
|
Try
If you get any results, something is listening and bound, eg
|
|||
|
|
Show
Show
Show
Show
e.g
to see if anything is listening on port localhost 25/TCP, or
to see if there are any sockets either local or remote either listening (local) or connected to (local or remote) for any host/interface |
||||
|
|
|
lsof (list open files) is a good tool to see if a process is listening on a port
netstat is a good tool for seeing if there are any active connections.
|
|||
|
|
|
You don't mention what protocol you want to use, i.e. TCP or UDP - and it's also important to realise that "port" isn't quite as granular the system supports to disambiguate sockets. E.g. if your system has multiple IP addresses then port 80 might be in use on all IP addresses (either the application has bound to "0.0.0.0" or "::" or to each IP address in succession), or it might be in use only on a subset of those IP addresses. The best, and surest, way to determine if a port/address is free and available is to attempt to bind to it. Netcat is handy for this. nc -l [-s a.b.c.d] -p NN will attempt to bind to TCP port NN on (optional, the default will be all addresses) a.b.c.d. Add the -u option to do the same in UDP. Next, to tell if the port is truly "open" as you ask - you need to start looking at potential firewall rules. Again the easiest thing is to try to connect to the port. Use netcat as above, on the server, and from a client use netcat to attempt to the connect to the port you opened. nc [-u] a.b.c.d NN will connect to port NN on a.b.c.d, using UDP if the -u flag is specified. You can then type input into the client end, and it should show up on the server. If it doesn't, you need to look into system and network specific tools. |
|||
|
|
|
I use fuser (in package psmisc):
Brings back the pid of process bound to this port. If this is to know if a port is listening, the good old telnet does the trick :)
|
|||
|
|
|
This one-liner will get you a list of all TCP ports in use. It works in bash on Ubuntu and OS X.
The list will have one port per line without any extra information. |
|||||
|