I am creating one bash script which can automate the process of checking the connectivity between two unix hosts. Need to check whether specific port is open from one host to other. hosts don't have NCAT(due to some constraint) so have to use Telnet only.
Code works fine if telnet connection is successful however takes more then 2 minutes if telnet fails. I need telnet to exit within 2 secs if it is not connecting. Trying to find a way to force the telnet command to exit even if it fails.
I have created couple of scripts which can read source file and run below command from there to telnet the destination servers.
Below code will be executed on the server from where telnet connectivity needs to be tested.
#!/usr/bin/bash
>/wastmp/$1.txt
echo "********************************************************************" >>/wastmp/$1.txt
echo "Checking Telnet connection from "$1 >>/wastmp/$1.txt
echo "********************************************************************" >>/wastmp/$1.txt
while read target;
do
x=$(sleep 3 | telnet `echo $target | awk 'BEGIN { FS=":" } { print $1 }'` ` echo $target | awk 'BEGIN { FS=":" } { print $2 }
'` | grep -i connected | awk {'print $1'})
if [[ "$x" == "Connected" ]];
then
echo $target"=Success" >>/wastmp/$1.txt
else
echo $target"=Fail, please check server & port">>/wastmp/$1.txt
fi
done </wastmp/target_server
Expected result: host1:port=Fail, please check server & port host2:port=Fail, please check server & port . . .
hostn:port=Success