I'm looking for a command or script which returns an unused port on my ubuntu linux system.
I have look on internet and the only thing I find is about the used/Listen port with the nestat command.
Apparently something with the netstat command will work but don't know what exactly.
Any idea how?
Thanks.
Best Answer
netstat -lat
gives the complete list of listening and established ports.When a port is not on any of those states doesn't exist for the system, so you won't find a command that shows the list of unused ports.
Keep in mind that there are 65535 ports, so anything that isn't on
netstat -lat
is an unused port.The following bash script will do a simple scan of tcp ports, and let you know which are open and which are closed :
If you save it as portscan.sh then it must be run as ./portscan.sh IP first_port last_port, for example:
./portscan 127.0.0.1 20 135
will scan the local equipment from ports 20 to 135