0

I need a script to check if the telnet is available on the server. I have a similar script for Linux but it is not working in HPUX.

Linux script,

telnet `"hostname"` < "dummy.txt" 2>&1 > telnet.txt ### passsing a dummy file ##
grep Escape telnet.txt >> telnet.txt
if [ $? = 0 ]; then
 echo "Telnet is available" > telnet.txt
 else
 echo "Telnet is not available" > telnet.txt
fi

Same script on Hpux

telnet `"hostname"` < "dummy.txt" 2>&1 > telnet.txt ### passsing a dummy file ##
grep Escape telnet.txt >> telnet.txt
if [ $? = 0 ]; then
 echo "Telnet is available" > telnet.txt
 else
 echo "Telnet is not available" > telnet.txt
fi

This command is not getting terminated, see below:

# telnet `"hostname"` < "dummy.txt" 2>&1 > telnet.txt
Telnet TERMINAL-SPEED option ON

That "Telnet TERMINAL-SPEED option ON" stopping the termiantion of the script. Required details captured in telnet.txt:

# cat telnet.txt
Trying...
Connected to ussltcsnh5001.solutions.glbsnet.com.
Escape character is '^]'.
Local flow control on

HP-UX ussltcsnh5001 B.11.31 U ia64 (ta)

login:
Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122
Godwin
  • 1
  • I think the TERMINAL-SPEED message is a red herring. Terminal speed negotiation is normal: https://tools.ietf.org/html/rfc1079 – HackSlash Nov 14 '17 at 17:49

1 Answers1

0

Does that dummy.txt contain a username and a password? Looks like you are stuck at the login prompt, which is why the command doesn't get terminated. You need to pass a username and password or otherwise exit an open session.

Try this example that will kill the telnet session after two seconds. You can increase the timeout here if you aren't getting the text you need.

EXAMPLE:

telnet `"hostname"` < "dummy.txt" 2>&1 > telnet.txt &
# Get its PID
PID=$!
# Wait for 2 seconds
sleep 2
# Kill it
kill $PID
grep Escape telnet.txt >> telnet.txt
if [ $? = 0 ]; then
 echo "Telnet is available" > telnet.txt
 else
 echo "Telnet is not available" > telnet.txt
fi
HackSlash
  • 287
  • 4
  • 15
  • Thanks for your response. Yes you're right script stukcs in login prompt and dummy.txt is an empty file. New script with sleep 1 added to it also didn't work, I need to do Ctrl+C to exit the command but however I am getting the output. If I'm putting it in middle of any long script I cannot do Ctrl +C to stop this. Please tell me if we have any other way. If we pass a dummy username and passowrd will the script teminates after the authentication failure? Kindly help me in sedning the dummy username and password to telnet. – Godwin Nov 15 '17 at 08:56
  • Can someone help me in passing username and password as a parameter for telnet login. – Godwin Nov 16 '17 at 09:14
  • I changed my answer because the previous method wasn't working for you. Please try the updated version. – HackSlash Nov 16 '17 at 16:10
  • Here is an expect script that you can use to automate the login: https://stackoverflow.com/questions/7013137/automating-telnet-session-using-bash-scripts – HackSlash Nov 16 '17 at 16:11