Help needed on If Else

Hi,

I am using "nc" command to test connectivity to Application ports.

I am getting following outputs in case of success & failed scenarios & I am re-directing it to a file.

FLPROVP02 [10.239.198.15] 4848 (?) open

FLPROVP02 [10.239.198.15] 4849 (?) : Connection refused

My script is as below.

#!/bin/bash
cd /root/nc_test/
nc -zv 10.239.198.15 4849 2> /root/nc_test/logfile2

I need help to write a If Else code that will help me to display "Port opened" & "Port cannot be reached" messages for the above 2 results.

Thanks,
Sunil

awk 'NF && /open[ \t]*$/{sub(/[ \t]*\(\?\).*/,"");print "Port opened for",$0;next}
NF && /Connection refused[ \t]*$/{sub(/[ \t]*\(\?\).*/,"");print "Port cannot be reached for",$0}' /root/nc_test/logfile2

producing

Port opened for FLPROVP02 [10.239.198.15] 4848
Port cannot be reached for FLPROVP02 [10.239.198.15] 4849

for your test file.

1 Like

Thank you for the help. It works fine now.:slight_smile: