Sample Header Ad - 728x90

Failing IF in a WHILE loop in BASH script that checks for open 22 ports

1 vote
3 answers
1867 views
Im trying to create a rather basic script to run through a list of servers and check if the SSH port is open using nc. I've tried a few different ways, but can't seem to get this to work. I am definitely not great at any type of scripting. Here is the script. I just want it to perform an action if it sees "succeeded" in the response from the nc command in OPEN. while read SERVER do OPEN=$(nc -z -v -w5 $SERVER 22) echo $SERVER if [[ $OPEN = *"suc"* ]]; then echo "Found SSH open on $SERVER" else echo "No open ports on $SERVER!" fi done < server.txt The list of servers is in the server.txt file that is referenced at the end on the script. Here is the response that I get. I not nc: connect to 10.10.51.55 port 22 (tcp) failed: No route to host 10.10.51.55 No open ports on test1! Connection to 10.10.51.65 22 port [tcp/ssh] succeeded! 10.10.50.65 No open ports in test2! It give me the "No open ports on $SERVER" no matter what. I thank you for any guidance.
Asked by saleetzo (590 rep)
May 10, 2018, 11:10 PM
Last activity: Apr 8, 2022, 11:33 AM