Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have numerous linux boxes with a very limited set of commands and disk space. But it has the telnet command on it.

I remotely connect to each of these probes (programmatically) and issue one line linux commands through SSH.

I need to a command to connect to a specific machine, using telnet, and then disconnect right away.

I can do all that, but the disconnection right away part. Telnet opens some sort of a console, or terminal and I can't figure out a one-line command to run the telnet command and then disconnect right away.

If I do that, I can easily parse the textual output for error messages for not being able to connect to the machine on the specified port and that's exactly what I am looking for.

So how can I run a one-line command to connect to a machine using telnet and disconnect afterwards ?

share|improve this question
    
As I understand your Telnet client does not support sending directly? –  IBr Aug 13 '13 at 11:36
    
@IBr, what do you mean ? –  Muhammad Gelbana Aug 13 '13 at 12:14
add comment

3 Answers 3

You ought to be able to pipe the exit command into STDIN in telnet. Try:

echo exit | telnet {site} {port}

and see if that works. (it seems to work on my web server, but YMMV).

share|improve this answer
add comment

The simplest and easiest method is given below.

 sleep <n> | telnet <server> <port>

n - The wait time in seconds before auto exit. It could be fractional like 0.5. Note that some required output may not be returned in the specified wait time. So we may need to increase accordingly.

server - The target server IP or hostname.

port - Target service port number.

You can also redirect the output to file like this,

sleep 1 | telnet <server> <port> > output.log
share|improve this answer
add comment

I think better tool for sending commands directly and just getting output would be netcat. It just simple, but powerful tool for putting commands through ports. You could see usage example in this superuser question: http://superuser.com/questions/261900/how-can-i-pipe-commands-to-a-netcat-that-will-stay-alive - asker gives working example in which connection closes after few seconds.

And if you want just to test connectivity use this: http://terminalinflection.com/use-netcat-not-telnet-to-test-network-connectivity/

share|improve this answer
    
I'm trying to install netcat right now but I'm unable to do so. I downloaded a compiled powerpc version but glibc library was missing. I downloaded a compiled powerpc version but the device didn't have enough space to copy the library files ! Is it possible that the glibc library already exists but netcat can't find it ? –  Muhammad Gelbana Aug 13 '13 at 12:13
    
Netcat is needed only on client side: on server (probe) can be the same plain old telnet. –  IBr Aug 13 '13 at 13:23
    
Also Glibc should exist already, it is used on much stuff in usual system. –  IBr Aug 13 '13 at 13:28
    
You could try to add to telnet scripts && exit see if it disconnects (in ssh at least it is enough). –  IBr Aug 13 '13 at 13:35
    
&& exit didn't work. If glibc should exist and I believe it does. Why would nc.traditional complain about it as if it's missing ? –  Muhammad Gelbana Aug 13 '13 at 14:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.