How to check if a Port is accepting connections.

Hi,
I need to create a script which checks the availability of a particular service on a particular Port on HP-Unix. Is there any command in unix wherein we can check if any port is accepting the connections now.

Thanks,
Vihang.

telnet

Right ... But I need to use this in a script such that telnet returns me some information that the connection was successfull and then I can use it for the rest of my script.

Can you please let me know how will I use telnet in the script for the above requirement?

Thanks,
Vihang.

#!/bin/ksh
LIST="apple:22
oracle:23
kiwi:9999
pear:21"
FILE="/tmp/telnet_port_status"
cat /dev/null >${FILE}
for HST in ${LIST};do
HOST=$(echo ${HST}|awk -F: '{print $1}')
PORT=$(echo ${HST}|awk -F: '{print $2}')
(telnet ${HOST} ${PORT} >/dev/null 2>&1) &
sleep 5
if [ $(ps -ef | grep -ce"telnet ${HOST} ${PORT}") -gt 1 ]; then
echo "telnet ${HOST} ${PORT} was good, ${PORT} was listening" >>${FILE}
else
echo "telnet ${HOST} ${PORT} was BAD, ${PORT} NOT listening" >>${FILE}
fi
ps -ef | grep -e"telnet ${HOST} ${PORT}"|grep -v grep|awk '{print "kill -9 "$2}' |/bin/sh >/dev/null 2>&1
done
cat ${FILE}

exit 0

netcat

nc -vz -w 10 ip port

just be aware that the [[:digit:]] will not work in hpux, nor do you really need it, you can take my bash code below and fill in your details.

#!/bin/bash

lolb=`awk '/[[:digit:]]/ { print $5}' /var/www/htdocs/vip/.form.php | cut -d \" -f 2 | grep [[:digit:]]`
EMAIL_TXT=/home/svcsuprt/email.txt
NEED_MAIL=0

# This is where we make the EMAIl_TXT data, first we remove the old one to ensure there is no munged data, 
# then add a header with some info in it.

cat /dev/null > /home/svcsuprt/email.txt
echo "Cannot connect the the following Load Balancers, please correct this asap as it impacts http://sitescope/vip" > $EMAIL_TXT
echo " " >> $EMAIL_TXT

# This is the main loop of the script that uses netcat to see if it can connect to 22, if it cannot, it goes in EMAIL_TXT

for ip in $lolb; 

        do /usr/bin/nc -vz -w 10 $ip 22;
                if [ $? -eq 1 ]
        then
                echo "$ip failed connectivity testing" >> $EMAIL_TXT; export NEED_MAIL=1
        fi; 
done;

## Send email

if [ ${NEED_MAIL} -eq 1 ]
        then
        cat /home/svcsuprt/email.txt | /bin/mailx -s "Load Balancer SSH test" email@tld.com

fi
MACHINE=8.8.3.201
PORT=2071
exec 3>/dev/tcp/${MACHINE}/${PORT}
if [ $? -eq 0 ]
then
    echo "Port accepting connections"
else
    echo " connections not possible"
fi