ksh behaves differently for telnet command

44 views Asked by At

I am trying to do telnet a Linux host inside ksh script, but the output seems to different when the same script is executed as bash script. Anyone has any Ideas ?

When the script when executed as bash

#!/bin/bash
echo -e '\x1dclose\x0d' | telnet myhost myport > /dev/null 2>&1
echo $?

output of $? is 0

Same script when executed as ksh results value of $? is 1

#!/bin/ksh
echo -e '\x1dclose\x0d' | telnet myhost myport > /dev/null 2>&1
echo $?

Any one have any ideas ?

Thanks in advance,

2

There are 2 answers

0
Ljm Dullaart On

The return value, of telnet is 1 if the remote host closes the connection (or any remote or network error occurs) and 0 if the local client side closes the connection successfully. My guess is, that bash terminates the client a bit quicker than ksh, so in the ksh version, the remote host may be a bit quicker closing the session. That being said, if you put some heavy load on your client system, the version in bash may also return a 1. The return value of telnet is not as helpful as you might think.

Also, telnet is an interactive program that is not really suited for the use in scripts. Use nc if the interaction is simple, or expect if the interaction becomes more complex.

0
Philippe On

printf \x behaves differently between ksh and bash, try to use octal notation

printf '\035close\x0d' | telnet myhost myport > /dev/null 2>&1