Question

From a bash script how can I quickly find out whether a port 80 is open/listening on a server.

On all workstations is opened daemon, but sometimes it fall, how i can check all files from file if port is open, i found a way to use

 nc -zw3 10.101.0.13 80 && echo "opened" || echo "closed"

but how to use it in bulk from a file, my writed file look like:

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 working one from non working, but also with all the line. Thank you

Was it helpful?

Solution 2

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

OTHER TIPS

You can use this bash one liner,

while IFS=";" read ip port; do nc -zw3 "$ip" "$port" && echo "$ip:$port => opened" || echo "$ip:$port =>  closed"; done<'ip_list_file.txt'

EDIT:

Try this:

while IFS=";" read ip port; do nc -zw3 "${ip}" "${port}" && echo "${ip}:${port} => opened" || echo "${ip}:${port} =>  closed"; done<'ip_list'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top