I'm using following shell script to check through set of ip and port from csv file. But it always breaking the while loop and only show the first result. But any how without error the script reaches the last line also. How to fix the loop breaking issue in telnet?
echo "starts"
while read p; do
if [ ! -z "$p" -a "$p" != " " ]; then
IP=`echo $p | cut -d',' -f1`
PORT=`echo $p | cut -d',' -f2`
TELNET_STR=`telnet "$IP" "$PORT" | grep "Connected"`
if [ ! -z "$TELNET_STR" -a "$TELNET_STR" != " " ]; then
echo '[success]:'$IP':'$PORT
else
echo '[failed]:'$IP':'$PORT
fi
fi
done <telnet.csv
echo "ends"
telnet.csv
234.253.245.23,80,1
234.089.108.216,8080,1
234.23.23.216,21,1
The telnet command is exit the parent shell, If its success or failure. You can use fork to run the commands in background.
() - To run the commands in sub shells.
& - Puts the function call in the background.
sleep - wait time for sub shell complete.
while read p; do
if [ ! -z "$p" -a "$p" != " " ]; then
IP=`echo $p | cut -d',' -f1`
PORT=`echo $p | cut -d',' -f2`
( sleep 2;
TELNET_STR=`telnet "$IP" "$PORT" | grep "Connected"`
if [ ! -z "$TELNET_STR" -a "$TELNET_STR" != " " ]; then
echo '[success]:'$IP':'$PORT
else
echo '[failed]:'$IP':'$PORT
fi
) &
fi
done <telnet.csv
Output
[success]:192.168.12.14:22
telnet: Unable to connect to remote host: Network is unreachable
[failed]:234.253.245.23:80
telnet: could not resolve 234.089.108.216/8080: Name or service not known
failed]:234.089.108.216:8080