11

I use the telnet command to check if MySQL port is responding.

telnet 10.10.10.24 3306

I use the ctrl character to disconnect. This is working as expected. How do I use this command in shell script?

shantanuo
  • 3,579
  • 8
  • 49
  • 66
  • Actually, do the echo with newlines as Janne propose, and add a sane timeout value with -w. – 3molo Aug 05 '11 at 09:34

7 Answers7

13

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
Cakemox
  • 25,209
  • 6
  • 44
  • 67
  • Thanks. But the -z switch is working on server but not working on another. Do I need to check the version of nc command? – shantanuo Aug 06 '11 at 04:41
  • Perhaps. What version is the one not working? – Cakemox Aug 06 '11 at 04:46
  • The -z switch is working correctly. I was wrong, ignore the above comment. – shantanuo Aug 07 '11 at 04:51
  • Is there any technique that we can achieve this with only a "telnet" command? I wrote one below but else condition is not working. for i in $(cat /tmp/ip_addresses.txt) do conn_test=$(echo "quit" | telnet "$i" 999 2> /dev/null | grep -oP 'Connected(?= to)') if [[ "${conn_test}" == "Connected" ]] ; then printf '%s\n' "Connection established" else printf '%s\n' "No Connection" fi done – KKE Jan 22 '21 at 20:39
9

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

echo -e "\n\n" | nc 10.10.10.24 3306
Janne Pikkarainen
  • 31,852
  • 4
  • 58
  • 81
5

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
Michał Šrajer
  • 856
  • 5
  • 11
2

To automate telnet script, you should use expect. See the expect home page.

jfg956
  • 1,116
  • 1
  • 8
  • 12
1

This is my script for any specific cases .

host=localhost
DATE=`date +%Y-%m-%d`
TIME=`date +%H%M%S`
LOG_OK=/tmp/telnet_ok
LOG_FAIL=/tmp/telnet_falha

for port in 80 25 22 443 110
do
if telnet -c $host $port </dev/null 2>&1 | grep -q Escape; then
  echo "$DATE $TIME  $port: Connected" >> $LOG_OK
else
  echo "$DATE $TIME $port : no connection" >> $LOG_FAIL
fi
done

http://fajlinux.com.br/2014/10/10/script-monitorando-via-telnet/

squillman
  • 37,883
  • 12
  • 92
  • 146
0

I would use netcat and it's '-w' instead;

host:~ user$  nc -w 1 1.2.6.1 3306
?
5.1.57-1~dotdeb.1?WO`rA*L#h?b4z.pmT;i~^;host:~ user$ 
3molo
  • 4,330
  • 5
  • 32
  • 46
0

Here is how to use telnet in bash shell/expect

#!/usr/bin/expect
# just do a chmod 755 one the script
# ./YOUR_SCRIPT_NAME.sh $YOUHOST $PORT
# if you get "Escape character is '^]'" as the output it means got connected otherwise it has failed

set ip [lindex $argv 0]
set port [lindex $argv 1]

set timeout 5
spawn telnet $ip $port
expect "'^]'."
grepit
  • 127
  • 5