1
How can I quickly find out whether port 80 is open/listening on a server via a bash script?
Normally on all workstations the daemon is running, but sometimes it fails. I use the following command to check if an IP address has an open port. I would like to be able to do this for every line in a file without having to manually type it each time.
nc -zw3 10.101.0.13 80 && echo "opened" || echo "closed"
The list file looks like this, where 3333
and 3334
are strings after $ip;
10.101.0.13; 3333
10.101.0.15; 3334
10.101.0.17; 4143
10.101.0.21; 1445
10.101.0.27; 2443
10.101.0.31; 2445
10.101.0.47; 3443
10.101.0.61; 3445
I have to separate open ports from closed ones, so I'll have a list of failed servers that will have to be fixed.
I tried something like this without success:
while IFS=";" read ip port ; do nc -zw3 "$ip" "$port" && echo "$ip:$port => opened" || echo "$ip:$port => closed" ; done < list.txt
while IFS=";" read ip port ; do nc -zw3 "${ip}" "${port}" && echo "${ip}:${port} => opened" || echo "${ip}:${port} => closed" ; done < list.txt
The wait flag isn't available/won't work as you intend here for every version of [tag:nc], can you try running one of your
nc -zw3
in shell with one open and one closed port and see if it really returns after 3 seconds? – None – 2014-01-29T09:41:10.617yes, with opened one i get response within 10 miliseconds
Connection to localhost 80 port [tcp/http] succeeded!
and with bad one, within the same 10 miliseconds i get empty line. – Mikhael Djekson – 2014-01-29T15:52:14.343What you describe works perfectly for me. Why do you say it's not working? – terdon – 2014-01-29T16:26:10.840