Using telnet in shell script

If you're just looking to check if the port is open, try:

$ nc -zv 10.10.10.24 3306
Connection to localhost 3306 port [tcp/mysql] succeeded!

nc will return 0 if the port is open and 1 if it is not. This is quite helpful for scripting as well. Omit the v switch to keep it quiet:

if ! nc -z 10.10.10.24 3306
then
    do_something
fi

nc is much better for non-interactive usage. Try something like

echo -e "\n\n" | nc 10.10.10.24 3306

If you don't have nc, you can use bash special files redirectons:

head -1 < /dev/tcp/10.10.10.24/3306 >/dev/null && echo MySQL is on || echo MySQL is off

Tags:

Shell