Vijay khurava
Vijay khurava

Reputation: 55

How to print pass/fail telnet results with ip and port number?

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

Answers (1)

Gaga Samushia
Gaga Samushia

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

Related Questions