Reputation: 55
I trying to print IP along with port no which is not connected or connected, currently I am getting results in two files but it does not telling me which is failed or completed on which port.
LOG_OK=/var/opt/Logs/port-check-success.log
LOG_FAIL=/var/opt/Logs/port-check-failed.log
for host in 1.1.11.1 2.2.2.1 5.6.61.3
do
for port in 80 443
do
if timeout 5 telnet -c $host $port </dev/null 2>&1 | grep -q Escape; then
echo "$port: Connected" >> $LOG_OK
else
echo "$port : no connection" >> $LOG_FAIL
fi
done
done
Upvotes: 0
Views: 240
Reputation: 155
You can try this
LOG_OK=/var/opt/Logs/port-check.log
for host in 1.1.11.1 2.2.2.1 5.6.61.3
do
checkp80=$(nmap -p 80 $host | grep 80 | awk '{print $2}')
checkp443=$(nmap -p 443 $host | grep 443 | awk '{print $2}' )
echo $host "Port80:"$checkp80 "Port443:"$checkp443 >> $LOG_OK
done
or with your original script
LOG_OK=/var/opt/Logs/port-check-success.log
LOG_FAIL=/var/opt/Logs/port-check-failed.log
for host in 1.1.11.1 2.2.2.1 5.6.61.3
do
for port in 80 443
do
if timeout 5 telnet -c $host $port </dev/null 2>&1 | grep -q Escape; then
echo "$host: $port: Connected" >> $LOG_OK
else
echo "$host: $port : no connection" >> $LOG_FAIL
fi
done
done
Upvotes: 1