Efficiently Test If a Port Is Open on Linux

Efficiently test if a port is open on Linux?

A surprise I found out recently is that Bash natively supports tcp connections as file descriptors. To use:

exec 6<>/dev/tcp/ip.addr.of.server/445
echo -e "GET / HTTP/1.0\n" >&6
cat <&6

I'm using 6 as the file descriptor because 0,1,2 are stdin, stdout, and stderr. 5 is sometimes used by Bash for child processes, so 3,4,6,7,8, and 9 should be safe.

As per the comment below, to test for listening on a local server in a script:

exec 6<>/dev/tcp/127.0.0.1/445 || echo "No one is listening!"
exec 6>&- # close output connection
exec 6<&- # close input connection

To determine if someone is listening, attempt to connect by loopback. If it fails, then the port is closed or we aren't allowed access. Afterwards, close the connection.

Modify this for your use case, such as sending an email, exiting the script on failure, or starting the required service.

Quick way to find if a port is open in all Local Network using Linux


I have to separate working one from non working, but also with all the
line.

you can just loop through the file, and write the status to corresponding text files

sed 's/;/ /' file | while read ip other
do
nc -zw3 $ip 80 && echo "$line" >> opened.txt || echo "$line" >> closed.txt
done

If you want to keep the IP; otherstuff format, you can use this one instead

while read line
do
ip=$(sed 's/;.*//' <<<"$line")
nc -zw3 $ip 80 && echo "$line" >> opened.txt || echo "$line" >> closed.txt
done < file

how to scan pool of ip if port is open or not? and list it


 sed "s/;//g" pool| while read ip string ;do  nc -zw3 $ip 80 || echo "$ip failed" >> report; done

sed will remove unwanted ; . while iterates through file . read splits fileline and extracts ip and nc reports only failed ips

how to check if a port is free in linux using c

i had same problem
the question is do you need to check just one port or many ports

If you need to check just one or few use bind, if it works then its free (and dont forget to free the socket)

if like me you need to check many ports, then what worked for me is run
system('netstat -tulpn') and redirect output to a file/variable
and then on this info search for ":{yourport}"

worked for me

ps
if like me you need to keep them free, tell your computer not to randomlly allocate ports in that area

list opened ports, close port and open port under centos

Use nmap to get the list of host open/close ports

nmap --top-ports 20 hostname

e.g
nmap --top-ports 20 localhost

Sample Image

Cross-platform check for port in use

While I'm certain the other answers would work in certain situations, I just wanted to note I ended up going with the following solution for my own work:

if echo PING | nc localhost 8080 >/dev/null; then
echo "Port 8080 in use"
fi

Fast way to test if a port is in use using Python

How about just trying to bind to the port you want, and handle the error case if the port is occupied?
(If the issue is that you might start the same service twice then don't look at open ports.)

This is the reasonable way also to avoid causing a race-condition, as @eemz said in another answer.



Related Topics



Leave a reply



Submit