Operating System - Linux
1752802 Members
5652 Online
108789 Solutions
New Discussion юеВ

telnet localhost here document

 
SOLVED
Go to solution
Shabu Khan-2
Frequent Advisor

telnet localhost here document


I am trying to get a script working on Red Hat Linux kernel 2.6, this works on HP-UX just fine but not on RHL.
# echo "SHUTDOWN" | telnet localhost 51001

works fine on HP-UX, it shuts down the process that is running on port 51001 and closes the port, this is the vendor recommended way, they don't want us to kill the PID but just do it this way.

the same one-liner on RHL has problems, it doesn't shutdown, it works if I do this:
# telnet localhost 51001
SHUTDOWN

But if I try to put it in a here document it wouldn't do it:
# telnet localhost 51001 <
6 REPLIES 6
Shabu Khan-2
Frequent Advisor

Re: telnet localhost here document

Anyone?
smatador
Honored Contributor

Re: telnet localhost here document

Hi,
I have no experience with a application stop in this condition, I just suggest you to check the xinetd.conf to see if telnet is enable, perhaps it could be the issue, or check if you have firewall..
Matti_Kurkela
Honored Contributor
Solution

Re: telnet localhost here document

I think I've seen similar behaviour once somewhere. Too bad I don't remember exactly what was the workaround/fix...

You might try something like this:

( echo "SHUTDOWN"; sleep 1 ) | telnet localhost 51001

MK
MK
Shabu Khan-2
Frequent Advisor

Re: telnet localhost here document

Thanks folks for responding.

telnet from the command line works so disabling telnet is not the problem, but Matti's solution worked and I am really surprised.

Matti,
Can you explain why a sleep command after the SHUTDOWN helped?
I really appreciate your assistance.

Thanks,
Matti_Kurkela
Honored Contributor

Re: telnet localhost here document

That was more interesting than I expected. Apparently this is related to the differences between BSD and System V, the two big branches of the family tree of unix-like systems.

RedHat's behaviour is actually a known (mis)feature of the BSD implementation of the telnet command, while the HP-UX telnet is probably a descendant of the System V implementation.

Google found for me a discussion about this exact problem on NetBSD network technology mailing list back in 1995:

http://www.google.com/search?q=site:mail-index.netbsd.org+batch+telnet+EOF&filter=0

When a BSD telnet command gets EOF from its input, it immediately aborts the entire connection instead of sending whatever it still has in its outgoing buffers, closing its socket and then waiting for the network side to send any final messages before exiting.

Apparently the BSD behaviour was not changed, as the change could have caused the telnet client to get stuck in an infinite loop at EOF.

When there is a sequence of commands inside parenthesis (a subshell) and the output of that sequence is piped somewhere, the pipe closes only after the last command in the sequence has completed. So adding short sleep after the echo command and enclosing the echo + sleep into a subshell sequence with parentheses will make the pipe to remain open for a while, and telnet has enough time to actually send the piped data.

Netcat ("nc") would probably be a better tool for tasks like this: telnet might in some situations pollute the outgoing data with telnet option negotiation codes, while nc is guaranteed to be clean. The netcat man page even mentions the telnet EOF problem.

echo "SHUTDOWN" | nc localhost 51001

MK
MK
Shabu Khan-2
Frequent Advisor

Re: telnet localhost here document

I am giving you ten more points for this great explanation.

Thanks Much!

Thanks,
Shabu